Search code examples
objective-cxcodecocoa-touchuipopovercontrollerpopover

App crashes when popover appears


I am trying to get a popover appear on a press of a button in ios 8, however the app crashes on the press of the button. I have set up all storyboards correctly and using breakpoints I determined that it is this line that causes the problem even though the view controller which is meant to be displayed in the popover is of FirstViewController class:

UIViewController *vc = destNav.viewControllers.firstObject;

This is the crash:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SecondViewController viewControllers]: unrecognized selector sent to instance 0x1446ea3a0'


This is the rest of my code from this part:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Assuming you've hooked this all up in a Storyboard with a popover presentation style
if ([segue.identifier isEqualToString:@"popover"]) {
    UINavigationController *destNav = segue.destinationViewController;
    FirstViewController *vc = destNav.viewControllers.firstObject;

    // This is the important part
    UIPopoverPresentationController *popPC = destNav.popoverPresentationController;
    popPC.delegate = self;
}
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}

Solution

  • I worked it out! Sorry that I am posting this quite late

    You simply need to exclude the line

     FirstViewController *vc = destNav.viewControllers.firstObject;
    

    even though all tutorials show this line