Dismiss UIPopoverController from subview

Closing an UIPopoverController from the subview, wether it is a UINavigationController any other type of controller can be a little bit of an hassle. I had this problem today and figured it out quickly. I thought of sharing this solution here.

The solution itself is quite simple. The ViewController creating the UIPopoverController is being reused to close it as well. This means that the UIPopoverController needs to be an accessible variable throughout that class.

So, how do we close it? well, this is how:

In the class opening the popover, this is my code:

So what I am doing here is creating a UINavigationController and setting a BookingDetailsViewController as its rootViewController. Then I am also adding the current class as delegate to this BookingDetailsViewController.

The second thing I added is a dismissal method called dismissPopoverAnimated:animated.

In my BookingDetailsViewController.h I added the following code:

And in my BookingDetailsViewController.m I added this code:

What happens is that when the “Close” button in the UINavigationController is pressed, the method closeView is called. This method check if the delegate responds to dismissPopoverAnimated:animated and if so, it calls it. If it does not respond to this method it will show a log message and do nothing more (so it wont crash).

I have written my code using ARC, hence there is no memory management.

I hope this helped you.

Posted in iPhone, Objective-c, Tutorials, XCode Tagged with: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *