Tried this but only works for UIButton:
[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
Tried this but only works for UIButton:
[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
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:
to
that, and declare btnClicked in the header file...
For what it's worth, this is how I setup a button in tabbarbuttonitem:
Set
target
andaction
of yourUIBarButtonItem
Swift 4
Just set the UIBarButtonItem's
target
andaction
properties directly.If you need this enough times in your code, it's nice to go ahead and extend
UIBarButtonItem
which I've done below in Swift. :)As an example, with self as a
UIViewController
, you'd simply call:If you are programmatically adding the UIBarButtonItem, the best way to set the target and action is to initialize the button with one of the following methods: