utsabiem utsabiem - 7 months ago 31
Objective-C Question

Pass argument in selector of right bar button item

I have this code:

UIBarButtonItem *donebutton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
self.navigationItem.rightBarButtonItem = donebutton;

Now I want to pass some argument to the method :

- (void)doneButtonPressed{}

How to do that ?


Use setAssociatedObject for this if u want to pass object say string then:

Firstly add

#import <objc/runtime.h>


NSString *strText = @"text";

 objc_setAssociatedObject(donebutton, "Argument", strText, OBJC_ASSOCIATION_RETAIN_NONATOMIC); // provide button , key , object for passing

and retrieve like this where ever u want your arguments:

NSString *str = objc_getAssociatedObject(donebutton, "Argument"); //using button and key
//remove object associated for button if not needed.

But if u want button reference then

- (void)doneButtonPressed:(id)sender{
  UIButton *btnClicked = (UIButton *)sender;