How to Dismiss Popovers in iOS
I wanted a quick edit field to display in a popover in an iPad app and then click done to have the popup disappear.Pretty standard, right?
8 hours later I was able to replicate the functionality. I hope these tips save you some time:
1: With XCode 4.2, you get Storyboards, which allow you to visually create a segue by right-clicking from the Button to the view you want to display. Use them.
If you click on the segue, you’ll have to give it a name and choose the Style (in our case, Popover).
Scott Sherwood has a great article on using Storyboards in Xcode 4.2.
2: Keep a reference to the segue before the segue is made.
1 2 3 4 5 6 |
|
Thanks to Symmetric on stackoverflow for the code above.
3: Dismissing the popover needs to happen in the delegate (the controller that put that popover on screen).
You may be tempted to write something like this:
1 2 3 4 5 6 7 |
|
The code you want is this:
1 2 3 4 |
|
However, what the hell is that editPopover I keep having in my code? That is the variable we need to have to keep track of the popover (though it seems like only a select few know this)
So, in your delegate header file define it:
1 2 3 |
|
Don’t worry about making it a property, synthesizing it, and all that jazz.
4: Yay! The popover Done button now works. Wait a minute, if you tap it multiple times, it creates multiple popovers…and then it won’t dismiss all of them. Wat?
Since you don’t want to ship with that weirdness, we need to keep track of the Done button with some more variables on our @interface:
1 2 3 |
|
Then, we want to add these lines to our prepareForSegue:
1 2 3 4 5 6 7 |
|
And, add these lines to our dismissPopover:
1 2 3 4 5 6 |
|
And, if someone taps outside of the popover to dismiss it, it should act the same way:
1 2 3 4 5 6 |
|