Search code examples
objective-cuipopovercontrolleruibarbuttonitem

Detect which UIBarButton was tapped from other ViewController


I have 4 UIBarButtons which show PopoverController when tapped. Depending upon which button tapped, I have to show different popover content and this has to be done in PopoverController viewDidLoad method.

Here is how I am handling popover from which UIBarButton tapped. in (VC1.m)

[PopoverController presentPopoverFromBarButtonItem:[self.navigationItem.rightBarButtonItems objectAtIndex:[self.navigationItem.rightBarButtonItems count] -1]
                                 permittedArrowDirections:UIPopoverArrowDirectionAny
                                                 animated:YES];

I want to put condition in viewDidLoad, (PopoverController.m)

- (void)viewDidLoad {
   [super viewDidLoad];
   if ([super.navigationItem.rightBarButtonItems objectAtIndex:[self.navigationItem.rightBarButtonItems count] -1]) {
      // Trigger method# 1
    }
    else {
      // Trigger method# 2
    }
}

Solution

  • I would suggest adding a property for the initial tab state that you are going to be using. basically:

    @property NSInteger tabState;
    

    Then you can either set this right after initialization, or in the init function.

    Assuming that PopoverController in your code above is a subclass of UIPopoverController which you are instantiating somewhere, you can either change the init function (usually – initWithContentViewController: to – initWithContentViewController:tabState: and then pass the tab state in during the your call to init:

    – initWithContentViewController:(UIViewController*)vc tabState:(NSInteger)newTabState
    {
         self = [super initWithContentViewController: vc];
         if (self) {
             self.tabState = newTabState;
         }
         return self;
    }
    

    Then in your -viewDidLoad method, you can just trigger off of your self.tabState and you have the state information without the tight binding to the prior view.