iosuipopoveruipresentationcontroller

Dismissing a Popover created by a segue on iOS 9


There are a lot of questions and answers on Stack Overflow about this but they are just valid for iOS 8 and before.

iOS 9 deprecated a lot of things and the answers on SO did not work anymore.

Said that, I am presenting a popover by performing a segue like this

[self performSegueWithIdentifier:@"myPopover" sender:self];

This segue is created between the current viewController and the viewController used by the popover. There is no button involved. The popover is anchored to a view.

The problem is that on prepareForSegue:identifier

[segue destinationViewController]

is a UIViewController and

[[segue destinationViewController] popoverPresentationController]

is the new UIPopoverPresentationController and this object does not offer a dismiss api anymore.

Instead, we are supposed to use

[self dismissViewControllerAnimated:YES completion:nil];

to dismiss the popover but this has no effect for me.

My situation is this: I have a popover with a text field. I want to dismiss the popover if the user hides the keyboard.

So I did this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

and then

- (void)keyboardWillHide:(NSNotification *)notification {
  [self dismissViewControllerAnimated:YES completion:nil];
}

but this has no effect at all.

I have also tried to create an unwind segue inside the popover viewController and call that from the presenting view controller but that makes the app crash.


Solution

  • Just tried it and it all seems to work perfectly.