是否可以手动执行Popover Segue(来自动态UITableView单元格)?

Hli*_*iev 30 uipopovercontroller ios segue

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

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

比我得到一个错误:

非法配置

Popover segue没有锚

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

joh*_*chd 62

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

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

    - (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];
    }
Run Code Online (Sandbox Code Playgroud)

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

  1. 首先,在视图控制器视图中创建一个隐藏的1px X 1px UIButton.(重要的是给出按钮约束,允许它在视图中的任何位置移动)
  2. 然后在视图控制器中为按钮(我称之为popOverAnchorButton)创建一个插座,并控制将一个segue从隐藏按钮拖动到您想要切换到的视图控制器.让它成为一个popOver segue.

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

现在只需在您的函数中手动调用您的segue.

    - (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];
    }
Run Code Online (Sandbox Code Playgroud)

而...... ......瞧.现在你正在使用segue的魔力和他们所有的伟大,你有一个动态的锚点,似乎指向你的细胞.现在在-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender你可以简单地将发送者强制转换为你的单元格的类(假设你对发送者类型进行了适当的检查以及调用了哪个segue)并给segue的destinationViewController提供了单元格的对象.

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

  • 此外,如果这有助于你.如果你可以投票.我是新用户,需要获得书呆子信用才能做任何事情. (7认同)