Reject Popover using Unwind Segue in Xcode Storyboard

I am using Xcode 4.5 and the new iOS 6 feature to unwind segues. I present a navigation preview controller inside a popover that is programmatically displayed from a panel button element:

- (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 defined a target to unwind segue (i.e. reject popover) ...

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

... and connected it to the cancel button on the navigation bar of the navigation controllers.

Connecting the cancel button to the cancelConfig button works fine in Xcode.

However, when you run the code, nothing happens when you click Cancel, despite the fact that Xcode 4.5 must support the rejection of popovers when unwinding segues (according to the release documents).

What did I miss?

Thanks!

+4
source share
1 answer

Unwind segues uses a run-time search by first asking the parent view controller to go to the chain of view controllers submitted through segue until it finds the correct unwinding method. But there is no chain here, since the popover was created programmatically and not using the popover segue.

No callbacks occur because there is no segue link back to the parent view controller. Unwind segues is an abstract form of delegation, so it would look like you didn’t forget to assign a delegate and not receive any callbacks.

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

Steps:

First, drag the control from the panel onto the view element in the view controller view onto the presented view controller and select the travel segment:

enter image description here

In view view controller, execute prepareForSegue: to get a link to the popover controller:

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

Then we implement shouldPerformSegueWithIdentifier: to restore the show / hide behavior is 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:

enter image description here

This will allow you to relax to cancelConfig: by clicking the cancel button from the popover, and also show / hide the popover when you click the button that represents it.

+5
source

Source: https://habr.com/ru/post/1447003/


All Articles