Ios – Segue Popover won’t behave properly

iossegueuistoryboard

I'm trying to use Segue to present then dismiss a Popover view a UIBarButtonItem is clicked.

I've created a generic Segue that is not anchored to anything but the view and given it a name

I've Anchored the UIBarButtonItem in the Interface Builder to:

- (IBAction)clickedSettings:(id)sender {

    if(self.popSegue != nil) {
        [self.popSegue.popoverController dismissPopoverAnimated:YES];
    } else {
        //Current says only a button may
        [self performSegueWithIdentifier:@"Settings" sender:sender];
    }
}

But when ever i click the button to display the Segue it gives me an error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.'

It doesn't even hit my -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

I've read the following questions on stack:

  1. iOS:How to dismiss Popover
  2. UIBarButtonItem + popover segue creates multiple popovers

But i still get the same error. For the life of me i can't figure out what is going wrong

Best Answer

I take no credit for this since I got every bit of it from going through multiple StackOverflow threads, but I got this working with:

@interface MyViewController ()
- (IBAction) toggleSettingsInPopover: (id) sender;
@property (nonatomic, strong) UIStoryboardPopoverSegue *settingsPopoverSegue;
@end

@implementation MyViewController
@synthesize settingsPopoverSegue = _settingsPopoverSegue;

- (IBAction) toggleSettingsInPopover: (id) sender {
    if( [self.settingsPopoverSegue.popoverController isPopoverVisible] ) {
        [self.settingsPopoverSegue.popoverController dismissPopoverAnimated: YES];
        self.settingsPopoverSegue = nil;
    } else {
        [self performSegueWithIdentifier: @"Settings" sender: sender];
    }
}

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender {
    if( [segue.identifier isEqualToString: @"Settings"] ) {
        if( [segue isKindOfClass: [UIStoryboardPopoverSegue class]] )
            self.settingsPopoverSegue = (UIStoryboardPopoverSegue *) segue;
        MySettingsViewController *msvc = segue.destinationViewController;
        msvc.delegate = self;
    }
}
@end

In my storyboard I control-dragged from my settings bar button item to MyViewController and connected it to the toggleSettingsInPopover action. Then I control-dragged from MyViewController to the view for the settings to create the segue, set its type to popover, set its identifier to Settings, set its directions to up and left (the toolbar is at the bottom of the screen and the button is at the right end), then dragged from its Anchor to the bar button item I connected to the action.

Related Topic