Using a split view on the iPad, I have the following code:
- (void) splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
barButtonItem.title = @"Categories";
NSMutableArray *items = [[toolbar items] mutableCopy];
[items insertObject:barButtonItem atIndex:0];
[toolbar setItems:items animated:YES];
[items release];
self.popoverController = pc;
}
This works well to show the popover when the button is pressed. However, I'd also like to have the popover dismiss if the button is pressed while it is already open to follow good guidelines. How would I go about doing this? (i.e. if the user repeatedly clicks this button, the popover should come and hide every other hit.)
The code I used to show the popover in RootViewController.m:
This I used to try and dismiss it from another class:
My
dismissPopover
function looks like:You could try the below
Apple's HIG says there should not be an explicit dismiss button inside a popover, but to do what you're asking, you have two options.
1) post an NSNotification
OR
2) drill down into your view hierarchy until you have the popover instance
1) in whichever view you are presenting the popover in, in the viewDidLoad method:
create a method called "dismissThePopover" and in the dealloc method, removeObserver
In your popoverController "dismiss" button, enter this line:
Doing that sends a notification to the app, and since you've registered your other view controller to listen for it, whenever it sees that notification it calls the selector you specify, in this case, dismissThePopover.
2) drill down into your view hierarchy to find self.popoverController
check this out, yours will be different, surely, but the overall idea is the same. Start at your AppDelegate, move into the first viewcontroller, move into subviews until you get to your self.popoverController object.
Hope this helps
This is a lot easier because the popoverController is a property. Makes it easier to reference.
I actually just realized that you're referring to the code inside the Delegate method for displaying the viewController at index:0 of your splitView. This answer doesn't necessarily apply to that, but does apply to any other time you're accessing and creating popoverControllers on the iPad. Without checking if a popover is visible first, you will either crash, or open several popovers.
Thanks for your time.
Yes, you can set the
modalPresentationStyle
as following:If you are using the default
UISplitViewController
setup, then the navigation bar button that is created displays a popover of yourRootViewController
.If you want to make sure you don't have multiple pop-ups on at once, you can simply dismiss pop-ups whenever your
RootViewController
will appear. Here's code I used to solve this problem: