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
All you need to do is inform your
UIBarButtonItemof the existence of theGameOptionsobject.That’s what you were doing using
selfin :[btn1 addTarget:self action:@selector(changeSettings:) forControlEvents:UIControlEventTouchUpInside];Just after instanciating your
UIBarButtonItem *buttonNav1, you can send it the messageaddTarget:action:forControlEvents:or, if you want to usesetAction, don’t forget to also usesetTarget.So now, your question is: what is the most elegant/efficient way to pass the
GameOptionsobject’s pointer to theUIBarButtonItem😉