vadim
vadim

Reputation: 119

Calling to function from UIBarButtonItem

I have button in my view with a setAction to a local function. in GameOptions.m i have the next function:

- (void) changeSettings (UIBarButtonItem *) sender  {

// do something 
}

and a button that start it in GameOptions.m :

[btn1 addTarget:self action:@selector(changeSettings:) forControlEvents:UIControlEventTouchUpInside];

now i moved it from his view to a navigation controller as an rightBrButtonItem. the myVC is an instance of small UIViewController with all the navigationBar settings,and each time i using pop ,push to it .

   UIBarButtonItem *buttonNav1 = [[UIBarButtonItem alloc] initWithCustomView:settingsView];
        [myVC.navigationItem setRightBarButtonItem:buttonNav1];

the problem is that this button is not in his GameOptions class and i want it to start the same function with implementation in GameOptions.m.

I would like to do the next thing :

buttonNav1 setAction:changeSettings;

but its impossible, the changeSetting is not recognized...

Tried : buttonNav1.action = self. but i'm not getting this function, only functions without parameters. How can i call to chnageSettings from that button in navigationBar

Upvotes: 0

Views: 564

Answers (1)

teriiehina
teriiehina

Reputation: 4761

All you need to do is inform your UIBarButtonItem of the existence of the GameOptions object. That's what you were doing using self in :

[btn1 addTarget:self action:@selector(changeSettings:) forControlEvents:UIControlEventTouchUpInside];

Just after instanciating your UIBarButtonItem *buttonNav1, you can send it the message addTarget:action:forControlEvents: or, if you want to use setAction, don't forget to also use setTarget.

So now, your question is: what is the most elegant/efficient way to pass the GameOptions object's pointer to the UIBarButtonItem ;)

Upvotes: 1

Related Questions