Dismiss Popover using Unwind Segue in Xcode Storyboard

一笑奈何 提交于 2019-12-04 09:38:44

Unwind segues use runtime searching by first asking the parent view controller to walk up the chain of view controllers presented via segue until it finds the correct unwind method. But there is no chain here since the popover was created programmatically rather than with a popover segue.

No callbacks are occurring since there is no segue link back to the parent view controller. Unwind segues are an abstracted form of delegation, so this would be similar to forgetting to set the delegate and not receiving any callbacks.

The solution is to create the popover with a segue in Interface Builder rather than create it programmatically with the configChartTapped: method.

Steps:

First, control-drag from the bar button item in the presenting view controller to the presented view controller and select the popover segue:

In the presenting view controller, implement prepareForSegue: to grab a reference to the popover controller:

- (void)prepareForSegue:(UIStoryboardPopoverSegue *)segue
                 sender:(id)sender {
    self.popover = segue.popoverController;
}

Then implement shouldPerformSegueWithIdentifier: to restore the show/hide behavior similar to configChartTapped::

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if (self.popover.isPopoverVisible) {
        [self.popover dismissPopoverAnimated:YES];
        return NO;
    } else {
        return YES;
    }
}

Finally, in Interface Builder, set the correct popover content size for the presented view controller:

This will allow you to unwind to cancelConfig: when tapping the cancel button from the popover, and also show/hide the popover when tapping the button that presents it.

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