views:

1676

answers:

2

Tried this but only works for UIButton:

[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

+2  A: 

Just set the UIBarButtonItem's target and action properties directly.

Ole Begemann
N.B. This only works if the `UIBarButtonItem` doesn't use a custom view, like a `UIButton`
Shaggy Frog
A: 

I ran into a similar problem... I assume you mean that if your UIButton is not part of your UITabBar to call btnClicked then it works appropriately. If this is the problem you are proposing then, check your btnClicked method and change it from:

-btnClicked:(id)sender

to

-(void) btnClicked:(id)sender

that, and declare btnClicked in the header file...

For what it's worth, this is how I setup a button in tabbarbuttonitem:

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
J. Dave
additionally, you can take out the ":" making...:@selector(btnClicked:)]; into ...:@selector(btnClicked)];
J. Dave