Search code examples
iosipaduipopovercontrollerpopover

iPad: Dismiss popover when button in navbar is tapped


I have a navigation bar that contains multiple buttons, including a back button, edit button and a button that opens a popover.

When the popover is open and the user taps any of the other buttons, I want the popover to close. I could try to detect every tap on all the buttons and intercept the action if the popover is open but I though there might be a more elegant option. It works with taps on all items that are not inside the navbar, only button in the navigation bar don't dismiss the popover.

Any suggestions?

My question is very similar to UIPopoverController does not dismiss when clicking on the NavigationBar but I don't seem to have a way to ask the author if he solved the problem.


Solution

  • Assign the selector of each button to the same method, first of all, check if the popover is open, then close it then redirect each button to it's method.

    -(IBAction) navButtons:(UIBarButtonItem *)sender {
         if(![popoverController isPopoverVisible] && sender.tag == 1){//assume that just one button will open the popover
             //present the popover
         } else {
             //dismiss the popover
         }
    
         switch (sender.tag) {
            case 1:
                 [self button1Handler];
                 break;
            case 2:
                 [self button2Handler];
                 break;
            /*...
              ...
              ...*/
            default:
                 break;
        }
    }
    

    I think this is the best solution you could use.