How to fire the action of a uibarbuttonitem programatically?
action, ios, uibarbuttonitem
Solution
This code worked with me
//in viewDidLoad
item = [[UIBarButtonItem alloc] initWithTitle:@"test" style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
toolBar.items = [NSArray arrayWithObject:item];
//In viewWillAppear
[item.target performSelector:item.action];
- (void) logout
{
NSLog(@"Called");
}
I dont know what you mean by click, but the above works
Problem
Possible Duplicate: How to fire uibarbuttonitem click event programmatically I have created an `uibarbuttonitem` dynamically and it works properly. I want to fire that `uibarbutton` item action(click) programatically for unit testing. Even though the code work properly when I log the action of the bar button item in the application code (not on testing code) it gives null. The code I have used is given below. ``` NSLog(@"%@",NSStringFromSelector(barButton.action)); ``` In the testing code I have created a bar button called logout and assign barbutton to that.To click the bar button item programatically I followed the following code. ``` [logout.target performSelector:logout.action]; ``` But it didn't work . I logged the action of logout button and it also gives null. ``` NSLog(@"%@",logout.action); ``` I want to know how to programatically click a `uibarbuttonitem` which created dynamically .