Absolute Ripple
  • Absolute Ripple
  • Contact

Disbling the button that triggers the Popover View

10/3/2012

0 Comments

 
Scenario: You have created a popover view using iOS storyboard. The popover view is triggered by the clicking of a 'button' on the UINavigationBar or UIToolbar. Once the popover view appears, you can click outside of the popover view to dismiss it. But if you click the 'button' again, another popover view appear on top of the existing one. To dismiss the popover views, you need to click outside the popover view twice to dismiss it. (Indeed, more popover view will appear if you click the 'button' again.) Not a nice behavior to have.

Disable the 'button'.
You need to disable the 'button' when the segue is triggered and re-enable it when the user's click outside the popover view.

1. Create two accessors using the usual @property & @synthesize approach.
Under @interface,
    @property (strong, nonatomic) IBOutlet UIBarButtonItem *btnSettings;
    @property (strong, nonatomic) UIStoryboardPopoverSegue* popSegue;
Under @implementation,
    @synthesize btnSettings = _btnSettings;
    @synthesize popSegue = _popSegue;
(Remember to link up the actual button to the IBOutlet in Interface Builder.)

2. Make the viewcontroller confirms to the UIPopoverControllerDelegate protocol
    eg. @interface ViewController : UIViewController <UIPopoverControllerDelegate>

3. Inside the method (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"NAME_OF_YOUR_SEGUE"]) {
        //- for this purpose alone, you don't really need a popSegue property, but having a reference
        //- to the popover segue is useful
        self.popSegue = (UIStoryboardPopoverSegue *)segue;
        //- this gets the reference to the actual UIPopoverController which you need to set the delegate
        UIPopoverController *pc = [_popSegue popoverController];
        //- setting the delegate to self
        //- this is important, otherwise the method under (4) won't be called and your button will remain disabled!
        pc.delegate = self;
        //- disable the button to prevent creating another popover
        [_btnSettings setEnabled:NO];
    }

4. Implement the following method in the UIPopoverControllerDelegate protocol,
    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
    {
        //- renable the setting button
        [_btnSettings setEnabled:YES];
    }
As the name suggests, this method gets call whenever the user click outsdie the popover view.

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.