You can make the popover view modal. Doing so prevents the user from clicking outside the popover view to dismiss the view. Of course, under this approach, you need to provide a mechanism to dismiss the popover.
The following assumes that we have a view (whose class is viewController) where a 'button' is located, and on clicking this button a popover view appears. This popoverview's class is popoverViewController.
1. Inside @implementation of the popover viewcontroller class file,
- (void)viewDidAppear:(BOOL)animated
{
self.modalInPopover = YES;
}
Note: self.modalInPopover must be in viewDidAppear. It won't work in viewDidLoad. Setting this property to 'YES' makes the view modal.
2. Create a protocol
Inside the popover view controller .h file, before @interface,
@protocol PopoverViewControllerDelegate;
Under @interface, create a property,
@property (nonatomic, weak) id<PopoverViewControllerDelegate>delegate;
After @end,
@protocol PopoverViewControllerDelegate <NSObject>
- (void)PopoverViewController:(PopoverViewController *)controller didClick:(BOOL)clicked;
@end
Next, synthesize the delegate. Inside the controller .m file, after @implementation,
@synthesize delegate;
3. You need to create a button that the user can click once they are finished doing whatever they need to do inside the popover view. This can be a button that triggers further actions such as saving some information that the user has entered or a 'cancel' button. But you need to provide a button as this allows you to link it to the mechanism that dismisses the popover.
Assuming that it is a 'Done' button, you could have a method like this in the popover view controller class file:
- (IBAction)btnDone:(id)sender {
[[self delegate] PopoverViewController:self
didClick:YES];
}
4. You need to obtain a reference to the popover segue so that you can dismiss the popover.
Under @interface of viewController.h,
@property (strong, nonatomic) UIStoryboardPopoverSegue* popSegue;
Synthesize this property in viewController.m,
@synthesize popSegue = _popSegue;
Inside (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender,
if ([segue.identifier isEqualToString:@NAME_OF_YOUR_SEGUE]) {
self.popSegue = (UIStoryboardPopoverSegue *)segue;
}
5. Finally, you need to implement the protocol inside the viewController class.
Import the header for PopoverViewController,
#import "PopoverViewController.h"
Make this class confirms to the PopoverViewControllerDelegate protocol, by adding <PopoverViewControllerDelegate> after @interface, such that it looks something like:
@interface ViewController: UIViewController <PopoverViewControllerDelegate>
Add the protocol method insider @implementation
- (void)PopoverViewController:(PopoverViewController *)controller didClicked:(BOOL )clicked
{
if ([self.popSegue.popoverController isPopoverVisible])
{
[self.popSegue.popoverController dismissPopoverAnimated:YES];
}
}