Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the action for a UIBarButtonItem in Swift

How can the action for a custom UIBarButtonItem in Swift be set?

The following code successfully places the button in the navigation bar:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil) self.navigationItem.rightBarButtonItem = b 

Now, I would like to call func sayHello() { println("Hello") } when the button is touched. My efforts so far:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:sayHello:) // also with `sayHello` `sayHello()`, and `sayHello():` 

and..

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(sayHello:)) // also with `sayHello` `sayHello()`, and `sayHello():` 

and..

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(self.sayHello:)) // also with `self.sayHello` `self.sayHello()`, and `self.sayHello():` 

Note that sayHello() appears in the intellisense, but does not work.

Thanks for your help.

EDIT: For posterity, the following works:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello") 
like image 221
kmiklas Avatar asked Jul 08 '14 20:07

kmiklas


People also ask

What is an action in Swift?

adjective. A swift event or process happens very quickly or without delay.

What is #selector in Swift?

Use Selectors to Arrange Calls to Objective-C Methods In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure, and you create them using the #selector expression.


2 Answers

As of Swift 2.2, there is a special syntax for compiler-time checked selectors. It uses the syntax: #selector(methodName).

Swift 3 and later:

var b = UIBarButtonItem(     title: "Continue",     style: .plain,     target: self,     action: #selector(sayHello(sender:)) )  func sayHello(sender: UIBarButtonItem) { } 

If you are unsure what the method name should look like, there is a special version of the copy command that is very helpful. Put your cursor somewhere in the base method name (e.g. sayHello) and press Shift+Control+Option+C. That puts the ‘Symbol Name’ on your keyboard to be pasted. If you also hold Command it will copy the ‘Qualified Symbol Name’ which will include the type as well.

Swift 2.3:

var b = UIBarButtonItem(     title: "Continue",     style: .Plain,     target: self,     action: #selector(sayHello(_:)) )  func sayHello(sender: UIBarButtonItem) { } 

This is because the first parameter name is not required in Swift 2.3 when making a method call.

You can learn more about the syntax on swift.org here: https://swift.org/blog/swift-2-2-new-features/#compile-time-checked-selectors

like image 127
drewag Avatar answered Sep 18 '22 04:09

drewag


Swift 4/5 example

button.target = self button.action = #selector(buttonClicked(sender:))  @objc func buttonClicked(sender: UIBarButtonItem) {          } 
like image 33
norbDEV Avatar answered Sep 18 '22 04:09

norbDEV