Absolute Ripple
  • Absolute Ripple
  • Contact

Making the Popover View Modal

2/3/2012

0 Comments

 
By default, popover view is dismissed when the user clicks anywhere outside of the view. Depending on your context, this may or may not be a desirable UI behavior.
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];
        }
    }
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    August 2013
    July 2013
    June 2013
    May 2013
    January 2013
    October 2012
    September 2012
    March 2012
    February 2012

    Categories

    All
    Apple Policy
    In App Purchase
    Ios Programming
    Our Apps

    RSS Feed

    Disclaimers

    Here are some resources that I find useful in writing iOS apps. These are not necessarily unique as they are based on various sources online.
    This is mainly to provide a source of reference to myself and my associates but if it can be of use to someone else, that is good.
    Note that information is provided here as is. Use at your own risk. There is no guarantee that it is accurate. We will update the information as time and resources permit.

    Powered by Create your own unique website with customizable templates.