Here is what I used previously,
var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action: Selector("menuButtonTapped:"))
But there is some syntax changes for Swift 3.
ex:-
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
Summarize the mostly used method in Swift 3 for adding action to a barButton.
Barbutton with text
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
BarButton with your own image
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"add"), style: .plain, target: self, action: #selector(addTapped))
BarButton with system image
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
If anyone is using customView
:
barButtonItem.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onBarButtonItemClicked)))
You just need to change your selector
syntax as of from Swift 3 you need to specify the first parameter name of method in your function call so change your selector like this.
#selector(menuButtonTapped(sender:))
And your method should be like this.
func menuButtonTapped(sender: UIBarButtonItem) {
}
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