Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire the action of a uibarbuttonitem programatically? [duplicate]

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 .

like image 721
Aruna Herath Avatar asked Feb 21 '23 00:02

Aruna Herath


1 Answers

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

like image 190
Omar Abdelhafith Avatar answered Apr 30 '23 13:04

Omar Abdelhafith