如何从iOS中的UICollectionViewCell初始化popup视图

我一直在跟着一个使用故事板的iPad应用程序跟踪,而且我一直坚持这个好几天。 如何通过在集合视图中select单元格来启动popup窗口? 主要的问题是越过必须锚定到视图的错误。

该方法似乎是在视图中放置一个虚拟popoverAnchorButton (隐藏,禁用),从它创build一个segue到故事板中的popup视图,将其放置在didSelectItemAtIndexPath ,然后[self performSegue] 。 代码如下所示:

 - (IBAction)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath [self.collectionView deselectItemAtIndexPath:indexPath animated:NO]; UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; CGRect anchorRect = CGRectMake(cell.center.x, cell.center.y, 1.0f, 1.0f); self.popoverAnchorButton.frame = anchorRect; [self performSegueWithIdentifier:@"popoverSegue" sender:self]; } 

这可以在应用程序中的其他位置在表格视图中工作,因为故事板让我在视图中放置一个button,以用作定位点。 但是Xcode不允许我在故事板的集合视图中放置一个button或任何其他合适的视图,所以我不能创buildsegue。 以编程方式创buildbutton没有任何帮助,因为我无法从中创buildUIStoryboardSegue,并且从控制器的任何手动延续都会给出关于缺less定位点的相同错误。 有任何想法吗?

我认为另一个path可能是跳过segues并以编程方式实例化popover视图控制器,但这里的障碍是由于我创build的popover视图(因为我使用的是故事板)没有xib而导致的错误。 我是否必须为这个popup视图创build一个单独的xib文件? 这是唯一的select吗?

如果您有兴趣在集合视图中获取当前所选单元格的CGRect ,可以使用:

 CGRect rect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame; 

之后,您可以使用您的presentPopoverFromRect:inView:permittedArrowDirections:animated:显示来自该rect的popup窗口。

是的,如果它有一个与之相关的故事板标识符,你总是可以从故事板dynamic加载一个VC:

 UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil]; UIViewController* vc = [storyboard instantiateViewControllerWithIdentifier:@"YourIdentifier"]; 

如果你从一个从故事板本身加载的VC调用代码,你可以使用:

 UIViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourIdentifier"]; 

快乐的编码!