Search code examples
iphonecocoa-touchdelegatesuibarbuttonitemrightbarbuttonitem

Delegate method called for rightBarButtonItem of UIBarButtonItem


What is the delegate method called when rightBarButtonItem of UIBarButtonItem is tapped? I want to implement a specific action there.


Solution

  • There is no predefined delegate method. You need to set the delegate/action (similar to a UIControl). For example, create the UIBarButtonItem in viewDidLoad the following way:

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Mark"                               style:UIBarButtonItemStylePlain  target:self action:@selector(actionForTap:) autorelease];
    

    and implement actionForTap: in your view controller. If you already have the UIBarButtonItem you can set the target/action to those of your desired delegate method, e.g.:

    self.navigationItem.rightBarButtonItem.target = self;
    self.navigationItem.rightBarButtonItem.action = @selector(actionForTap:);
    

    As a third way, you can configure it in IB (but I'm not going there).