如何从iOS中的UICollectionViewCell初始化弹出视图 [英] How to initialize popover view from UICollectionViewCell in IOS

查看:266
本文介绍了如何从iOS中的UICollectionViewCell初始化弹出视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用情节提要和iPad应用程序进行跟踪,而且我已经坚持了好几天.如何通过在集合视图中选择一个单元来启动弹出窗口搜索?主要问题是克服了必须将弹出框锚定到视图的错误.

I've been tracking along with an iPad app using storyboards, and I've been stuck on this for days. How can I initiate a popover segue by selecting a cell in a collection view? The main problem is getting past the error that the popover must be anchored to a view.

该方法似乎是在视图中放置一个虚拟popoverAnchorButton(隐藏,禁用),从该视图创建一个脚本到情节提要中的弹出视图,将其放置在didSelectItemAtIndexPath中,然后放置在[self performSegue]中.代码如下:

The approach seems to be putting a dummy popoverAnchorButton (hidden, disabled) in the view, create a segue from it to the popover view in the storyboard, position it in didSelectItemAtIndexPath, and then [self performSegue]. Code looks like this:

- (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];
}

这可以在表格视图中应用程序的其他位置使用,因为情节提要允许我在视图中放置一个按钮以用作定位点.但是Xcode不允许我将一个按钮或任何其他合适的视图放到情节提要中的集合视图中,因此我无法创建segue.以编程方式创建按钮无济于事,因为我无法从中创建UIStoryboardSegue,并且从控制器进行的任何手动segue都会给出关于缺少锚点的相同错误.有什么想法吗?

This works elsewhere in the app in a table view, because the storyboard lets me drop a button in the view, to use as an anchor point. But Xcode doesn't let me drop a button or any other suitable view into the collection view in the storyboard, so I can't create the segue. Creating the button programmatically is no help, because I can't build a UIStoryboardSegue from it, and any manual segue from the controller gives the same error about lacking an anchor point. Any ideas?

我认为另一种方法可能是跳过测试并以编程方式实例化弹出视图控制器,但是这里的障碍是由于我创建的弹出视图(因为我使用情节提要)没有xib而产生的错误.我是否必须为此弹出窗口视图创建一个单独的xib文件?那是唯一的选择吗?

I think another path could be to skip segues and instantiate the popover view controller programmatically, but the roadblock here is an error stemming from the fact that the popover view I create (since I'm using storyboards) has no xib. Do I have to create a separate xib file just for this popover view? Is that the only option?

推荐答案

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

If you are interested in getting the CGRect of the currently selected cell in the collection view you might use:

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

然后,您可以使用UIPopoverController的presentPopoverFromRect:inView:permittedArrowDirections:animated:显示来自rect的弹出窗口.

And after that you can display your popover from that rect using presentPopoverFromRect:inView:permittedArrowDirections:animated: of your UIPopoverController.

是的,如果具有关联的情节提要标识符,则始终可以从情节提要中动态加载VC:

And yes, you can always dynamically load a VC from your storyboard if it has a storyboard identifier associated to it:

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

如果您是从故事板本身加载的VC中调用代码,则可以使用:

In case you are calling the code from a VC loaded from storyboard itself, instead you can use:

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

这篇关于如何从iOS中的UICollectionViewCell初始化弹出视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆