是否可以手动执行Popover Segue(来自动态UITableView单元格)? [英] Is it possible to perform a Popover Segue manually (from dynamic UITableView cell)?

查看:168
本文介绍了是否可以手动执行Popover Segue(来自动态UITableView单元格)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户触摸动态TableView中的单元格时,我需要执行Popover segue。但当我尝试使用此代码执行此操作时:

I need to perform a Popover segue when user touches a cell in a dynamic TableView. But when I try to do this with this code:

- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        [self performSegueWithIdentifier:@"toThePopover" sender:[tableView cellForRowAtIndexPath]];
    //...
} 

比我收到错误:

非法配置


没有锚点的Popover segue

Popover segue with no anchor

有没有办法做到这一点(手动从动态TableView执行popover segue)?

Is there any way to do this (to perform a popover segue from dynamic TableView manually)?

推荐答案

今晚我遇到了同样的问题,有几个解决方法(包括用老式的方式呈现弹出窗口)。

I was faced with this same issue tonight, there a couple workarounds (including presenting the popover the old fashioned way).

For这个例子,我有一个存储在我的自定义单元类中的对象。当选择单元格时,我调用这样的函数在popOverViewController中打开关于对象的细节,并指向(锚定)它在表格中的相应单元格。

For this example, I have an object that is stored in my custom cell class. When the cell is selected I call a function like this to open details in a popOverViewController about the object, and point (anchor) to it's corresponding cell in the table.

    - (void)openCustomPopOverForIndexPath:(NSIndexPath *)indexPath{
        CustomViewController* customView = [[self storyboard] instantiateViewControllerWithIdentifier:@"CustomViewController"];

        self.myPopOver = [[UIPopoverController alloc]
                                   initWithContentViewController:customView];
        self.myPopOver.delegate = self;
        //Get the cell from your table that presents the popover
        MyCell *myCell = (MyCell*)[self.tableView cellForRowAtIndexPath:indexPath];
        CGRect displayFrom = CGRectMake(myCell.frame.origin.x + myCell.frame.size.width, myCell.center.y + self.tableView.frame.origin.y - self.tableView.contentOffset.y, 1, 1);
        [self.myPopOver presentPopoverFromRect:displayFrom
                                             inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
    }

这种方法的问题在于我们经常需要弹出视图来拥有自定义初始化器。如果您希望视图设计在storyboard而不是xib中,并且有一个自定义init方法,将您的单元格关联对象作为参数用于显示,则会出现问题。你也不能只使用一个popover segue(乍一看),因为你需要一个动态锚点(并且你不能锚定到一个单元原型)。所以这就是我所做的:

The problem with this method is that we often need the popover view to have a custom initializer. This is problematic if you want your view to be designed in storyboard instead of a xib and have a custom init method that takes your cells associated object as a parameter to use for it's display. You also can't just use a popover segue (at first glance) because you need a dynamic anchor point (and you can't anchor to a cell prototype). So here is what I did:


  1. 首先,在视图控制器视图中创建一个隐藏的1px X 1px UIButton。 (重要的是给出允许它在视图中的任何位置移动的按钮约束)

  2. 然后在视图控制器中创建按钮的出口(我称之为我的popOverAnchorButton)并控制拖动从隐藏按钮到您想要切换到的视图控制器的segue。将它设为popOver segue。

现在你有一个带有'合法'锚点的popover segue。按钮被隐藏,所以没有人可以意外触摸它。你只是用它作为一个锚点。

Now you have a popover segue with a 'legal' anchor. The button is hidden, so no one can touch it accidentally. You are only using this for an anchor point.

现在只需在你的函数中手动调用你的segue。

Now just call your segue manually in your function like this.

    - (void)openCustomPopOverForIndexPath:(NSIndexPath *)indexPath{
        //Get the cell from your table that presents the popover
        MyCell *myCell = (MyCell*)[self.tableView cellForRowAtIndexPath:indexPath];

        //Make the rect you want the popover to point at.
        CGRect displayFrom = CGRectMake(myCell.frame.origin.x + myCell.frame.size.width, myCell.center.y + self.tableView.frame.origin.y - self.tableView.contentOffset.y, 1, 1);

        //Now move your anchor button to this location (again, make sure you made your constraints allow this)
        self.popOverAnchorButton.frame = displayFrom;
        [self performSegueWithIdentifier:@"CustomPopoverSegue" sender:myCell];
    }

而且...... Voila。现在你正在使用segue的神奇之处,并且你有一个看起来像你的细胞的动态锚点。
现在在 - (void)prepareForSegue:(UIStoryboardSegue *)segue发送者:(id)发件人你可以简单地将发送者投射到你的单元格的类中(假设你对发送者类型进行适当的检查以及调用哪个segue)并给segue的destinationViewController提供单元格的对象。

And...... Voila. Now you are using the magic of segues with all their greatness and you have a dynamic anchor point that appears to point to your cell. now in -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender you can simply cast the sender to your cell's class (given that you do the proper checks on sender type and which segue is being called) and give the segue's destinationViewController the cell's object.

如果这有帮助,或者任何人有任何反馈,请告诉我。或改进。

Let me know if this helps, or anyone has any feedback or improvements.

这篇关于是否可以手动执行Popover Segue(来自动态UITableView单元格)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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