How to dismiss UIPopoverControllers that I don't have reference to?
I have a number of BarButtons in my toolbar, and I want to show a different UIPopoverController for each of them. When I click on one o开发者_运维百科f them, other PopoverControllers should be dismissed (i.e. only one popovercontroller is shown on the screen). I don't want to keep references to them -- because that's too annoying. Is there another way to dismiss them?
Thanks.
There's no "dismiss all popovers" function that I know of.
But to solve this, you don't have to keep references to all of your different popovers; only keep a reference to the currently showing popover. Then when a new popover is launched, you can dismiss that currently showing popover (if it is not nil). Then assign "currently showing popover" to the new popover you display.
Using the automated testing framework KIF brought me to the idea to look at their dismissPopovers function.
I modified this function a little bit, so you could really use a global dismiss all popovers function. Here is the code:
//dismiss popovers
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIView *currentPopover in [[windows objectAtIndex:0] subviewsWithClassNamePrefix:@"UIDimmingView"]) {
[currentPopover removeFromSuperview];
}
And the UIViewExtension:
- (NSArray *)subviewsWithClassNamePrefix:(NSString *)prefix;
{
NSMutableArray *result = [NSMutableArray array];
// Breadth-first population of matching subviews
// First traverse the next level of subviews, adding matches.
for (UIView *view in self.subviews) {
if ([NSStringFromClass([view class]) hasPrefix:prefix]) {
[result addObject:view];
}
}
// Now traverse the subviews of the subviews, adding matches.
for (UIView *view in self.subviews) {
NSArray *matchingSubviews = [view subviewsWithClassNamePrefix:prefix];
[result addObjectsFromArray:matchingSubviews];
}
return result;
}
Thanks to KIF for the hints
As per the developers document from here Taps outside of the popover’s contents automatically dismiss the popover. but you can use this method dismissPopoverAnimated
to dismiss the popover programmatically in response to taps inside the popover window.Like for example you can dissmiss it in didFinishPickingMediaWithInfo
delegate of UIImagePickerController
Hope you get it...Good Luck!
精彩评论