Popover segue to static cell UITableView causes co

2020-08-26 04:30发布

问题:

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:

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...

回答1:

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.



回答2:

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.



回答3:

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)?



回答4:

As of iOS 10, @lehn0058's correct and accepted answer no longer works. Here's his solution updated for 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: {})
}