Dismiss Popover using Unwind Segue in Xcode Storyboard

倾然丶 夕夏残阳落幕 提交于 2019-12-06 05:54:28

问题


I am using Xcode 4.5 and the new iOS 6 feature to unwind segues. I am presenting a navigation view controller inside a popover which is presented programmatically from a bar button item:

- (IBAction)configChartTapped:(id)sender
{
    if (self.popover.isPopoverVisible) {

        [self.popover dismissPopoverAnimated:YES];

    } else {
        UINavigationController *chartConfigNavigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"GrowthChartNavigationController"];

        ConfigChartTypeViewController *configChartTypeViewController = (ConfigChartTypeViewController*) chartConfigNavigationController.topViewController;

        self.popover = [[UIPopoverController alloc]initWithContentViewController:chartConfigNavigationController];
        self.popover.popoverContentSize = CGSizeMake(320, 500);
        self.popover.delegate = self;

        [self.popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

Next to this method I have defined a target to unwind the segue (i.e. dismissing the popover)...

- (IBAction)cancelConfig:(UIStoryboardSegue *)segue
{
    //
}

... and connected it to a cancel button in the navigation view controllers's navigation bar.

Connecting the cancel bar button to the cancelConfig button worked fine in Xcode.

However, when running the code, nothing happens when clicking on the Cancel button despite Xcode 4.5 should be supporting dismissing popovers when unwinding segues (according to the release docs).

What did I miss?

Thank you!


回答1:


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.



来源:https://stackoverflow.com/questions/13457296/dismiss-popover-using-unwind-segue-in-xcode-storyboard

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