I have a problem dismissing a popover that was launched from the navigationItem of a UINavigationController. It seems that the navigation item which is inserted by the UINavigationController does not trigger dismissal of the UIPopoverController. Usually, when you tap outside a popover, it is dimissed. But when you tap on the navigation item, the popover is not dismissed. Worse, if you tap the button which triggers the popover, you’ll get a second instance of the popover.
All of this is done using storyboards:
– Create a view, embed it into a UINavigationView so it gets a navigationItem on the top.
– Put a UIBarButtonItem into the navigationItem (left or right, doesn’t matter for the initial view on the navigation stack).
– Define another view, and drag a segue from the UIBarButtonItem to this view.
– Set the segue to be popover.
Once the popover is open, I cannot dismiss it by tapping on the navigationItem. I cannot believe this “works as designed”, so I suspect I missed out on somthing.
Although my goal is to program as little as possible (that’s what storyboards are about, aren’t they?), I thought about workarounds: The first workaround that came to my mind was to add a UITapGestureRecognizer to the navigationItem, which would dismiss the popover when it detected a tap on the navigationItem. Unfortunately, the navigationItem seems to not be UIVIew, so it lacks the addGestureRecognizer: method…
EDIT: Adding a UITapGesturerecognizer to self.navigationController.navigationBar is possible, but prevents any tap to reach the UIBarButtonItems on the navigationBar. Yes, I could have expected that.
Thanks a lot for help,
nobi
Here’s the complete description for popovers in storyboards. Assuming your controller to appear in the popover is named
MyPopupController, do this:MyPopupControllerscene, using stylePopover.Add a property to
MyPopupController.hSynthesize the property in
MyPopupController.mIn the
prepareForSegue:sender:method of the main scene controller, set up thepopoverproperty:In the
viewWillAppear:method ofMyPopupController, add the following code (don’t forget[super viewWillAppear]as well):You should be done now.
Thanks to Robert for the basic idea – I just had to find the correct place because
presentPopoverFromBarButtonItemis not used when using storyboards. Note that putting (5) intoviewDidLoaddoes not work.Have fun coding!