Tried this but only works for UIButton:
[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
Just set the UIBarButtonItem's target
and action
properties directly.
UIBarButtonItem doesnt have the same addTarget method so you have to set them directly as follows
btn.target = self;
btn.action = @selector(barButtonCustomPressed:);
...
// can specify UIBarButtonItem instead of id for this case
-(IBAction)barButtonCustomPressed:(UIBarButtonItem*)btn
{
NSLog(@"button tapped %@", btn.title);
}
Set target
and action
of your UIBarButtonItem
Swift 5 & 4
button.target = self
button.action = #selector(action)
@objc func action (sender:UIButton) {
print("action")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With