Delegate method called for rightBarButtonItem of UIBarButtonItem

5.5k views Asked by At

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

2

There are 2 answers

0
fsaint On BEST ANSWER

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).

0
Ashvin On

You don't need any delegate method. You can simply use the below code. This is code for simply adding a button on the right and adding an action to that button. In my case I called the method named "AddButtonMethod".

UIBarButtonItem *AddButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddButtonMethod:)];
self.navigationItem.rightBarButtonItem = AddButton;