Popover segue to static cell UITableView causes compile error

风流意气都作罢 提交于 2019-12-05 02:47:05

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.

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.

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

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: {})
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!