How to set an action for an UIBarButtonItem? How to set an action for an UIBarButtonItem? xcode xcode

How to set an action for an UIBarButtonItem?


Select the UIBarButton , Create an IBAction by CNRL+Dragging at the implementation section in .m file then try to print with NSLog use breakpoint to know if the control is reaching the IBAction you created.


Here is how you do it with Swift 3:

let check: UIBarButtonItem = UIBarButtonItem()check.title = "Check"check.width = CGFloat(100)check.tintColor = UIColor.greencheck.action = #selector(ViewController.checkTapped)check.target = selffunc checkTapped (sender:UIBarButtonItem) {    print("check pressed")}


myBarButtonItem.target = self;myBarButtonItem.action = @selector( myMethod: );

Remember action method must have the following signature:

- ( IBAction )myMethod: ( id )sender;

or refer this link : Add a custom selector to a UIBarButtonItem