xcode Popover segue 到静态单元格 UITableView 导致编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12229243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:23:57  来源:igfitidea点击:

Popover segue to static cell UITableView causes compile error

xcodeios5uitableviewuistoryboardsegue

提问by lehn0058

I currently have an application with two view controllers. The first is a view controller with an embedded table view that has dynamic cells. The second is a table view controller with static cells. If I add a segue from selecting one of the dynamic table's cells to the static table view controller (using the Push or Modal style setting), I can see that the segue works as expected. However, when I change the style to Popover I get the following compile error:

我目前有一个带有两个视图控制器的应用程序。第一个是带有动态单元格的嵌入式表格视图的视图控制器。第二个是带有静态单元格的表视图控制器。如果我从选择动态表的单元格之一到静态表视图控制器(使用 Push 或 Modal 样式设置)添加 segue,我可以看到 segue 按预期工作。但是,当我将样式更改为 Popover 时,出现以下编译错误:

Couldn't compile connection: <IBCocoaTouchOutletConnection:0x4004c75a0 <IBProxyObject: 0x400647960> => anchorView => <IBUITableViewCell: 0x400f58aa0>>

Has anyone else ran into this issue, or does anyone know what this error message might mean? It seems strange that this is happening at compile time unless a static table view controller is not supported in a Popover...

有没有其他人遇到过这个问题,或者有没有人知道这个错误信息可能意味着什么?除非 Popover 不支持静态表视图控制器,否则这在编译时发生似乎很奇怪......

回答by lehn0058

I figured out how to do this. You can't hook it up from the storyboard but can do it programmatically like this:

我想出了如何做到这一点。您无法从故事板中连接它,但可以像这样以编程方式进行操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad"
                                                 bundle:nil];
    UITableViewController *detailController = [sb instantiateViewControllerWithIdentifier:@"TableSettingDetails"];

    self.popoverController = [[UIPopoverController alloc] initWithContentViewController:detailController];

    self.popoverController.popoverContentSize = CGSizeMake(320, 416);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self.popoverController presentPopoverFromRect:cell.bounds inView:cell.contentView
                          permittedArrowDirections:UIPopoverArrowDirectionAny
                                          animated:YES];
}

Just make sure that you have a reference to your popover in your controller, otherwise it will get immediately disposed - causing some other interesting exceptions.

只需确保您在控制器中引用了您的弹出框,否则它将立即被处理 - 导致一些其他有趣的异常。

回答by LJ Wilson

You have to choose an anchor point for that Popover that is NOT the static cell. My suggestion is to put a UIButton set to be invisible (Custom type). Then select the Popover Segue and drag the Anchor connection to that button.

您必须为该 Popover 选择一个不是静态单元格的锚点。我的建议是将 UIButton 设置为不可见(自定义类型)。然后选择 Popover Segue 并将 Anchor 连接拖到该按钮上。

回答by johnrechd

I know this one is already answered, but just incase it is helpful I have a solution for this while preserving the storyboard flow using segues. You can check it out here Is it possible to perform a Popover Segue manually (from dynamic UITableView cell)?

我知道已经回答了这个问题,但以防万一它有帮助,我有一个解决方案,同时使用 segues 保留故事板流程。您可以在此处查看 是否可以手动执行 Popover Segue(从动态 UITableView 单元格)?

回答by Grimxn

As of iOS 10, @lehn0058's correct and accepted answer no longer works. Here's his solution updated for iOS 10...

从 iOS 10 开始,@lehn0058 的正确答案不再有效。这是他为 iOS 10 更新的解决方案......

override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
    // *** Next line doesn't work with popover, only full screen detail
    //self.performSegue(withIdentifier: "editRow", sender: self) 
    // Hence, do it by hand...
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let detailVC: MyDetailViewController = sb.instantiateViewController(withIdentifier: "itemEditor") as! MyDetalViewController
    detailVC.modalPresentationStyle = .popover
    detailVC.popoverPresentationController?.sourceView = tableView.cellForRow(at: indexPath)
    detailVC.detailItem = self.itemAtIndexPath(indexPath)

    self.present(detailVC, animated: true, completion: {})
}