98

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")
3
  • 4
    you pass selectors in swift by just putting the selector in a string, action: "sayHello"
    – Jiaaro
    Jul 8, 2014 at 20:53
  • Thank you so much. I'm under pressure to get this out and was getting frustrated.
    – kmiklas
    Jul 8, 2014 at 20:59
  • 1
    This question was previously marked as a duplicate of @selector() in Swift?. However, this question asks specifically about UIBarButtonItem while the other does not. Requiring beginners to generalize all uses of selector can be difficult for them, so I am removing the duplicate status so that people can keep this question up to date.
    – Suragch
    Nov 30, 2016 at 0:20

6 Answers 6

161

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

7
  • 11
    Just to mention that the action function cannot be private! I am not really sure why, but I always get an error in case I provided the name of some private function
    – maddob
    Jul 8, 2015 at 8:46
  • I got this: "does not implement methodSignatureForSelector"
    – AlamoPS
    Jul 25, 2015 at 23:53
  • 1
    @AlamoPS If you're using the 2nd version with the colon, you'll need to make sure the function parameter types match that for whatever action you're handling.
    – Jason
    Aug 18, 2015 at 15:46
  • 1
    @RyanForsyth The string actually gets translated into Selector("sayHello") behind the scenes by Swift anyway.
    – fatuhoku
    Sep 22, 2015 at 9:10
  • 3
    This answer is outdated. Jul 8, 2016 at 20:15
44

Swift 4/5 example

button.target = self
button.action = #selector(buttonClicked(sender:))

@objc func buttonClicked(sender: UIBarButtonItem) {
        
}
4
  • 6
    This is right, for Swift 4 you need to add @objc to the function declaration. Up until Swift 4 this was implicitly inferred. Jun 18, 2018 at 18:19
  • 1
    button.target = self
    – MD.
    May 22, 2020 at 7:24
  • @Mahesh don't need to set the target
    – norbDEV
    May 22, 2020 at 14:18
  • 1
    @norbDEV target is needed for Swift 5
    – Biasi Wiga
    Sep 27, 2020 at 7:11
6

Swift 5 & iOS 13+ Programmatic Example

  1. You must mark your function with @objc, see below example!
  2. No parenthesis following after the function name! Just use #selector(name).
  3. private or public doesn't matter; you can use private.

Code Example

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    let menuButtonImage = UIImage(systemName: "flame")
    let menuButton = UIBarButtonItem(image: menuButtonImage, style: .plain, target: self, action: #selector(didTapMenuButton))
    navigationItem.rightBarButtonItem = menuButton
}

@objc public func didTapMenuButton() {
    print("Hello World")
}
0

May this one help a little more

Let suppose if you want to make the bar button in a separate file(for modular approach) and want to give selector back to your viewcontroller, you can do like this :-

your Utility File

class GeneralUtility {

    class func customeNavigationBar(viewController: UIViewController,title:String){
        let add = UIBarButtonItem(title: "Play", style: .plain, target: viewController, action: #selector(SuperViewController.buttonClicked(sender:)));  
      viewController.navigationController?.navigationBar.topItem?.rightBarButtonItems = [add];
    }
}

Then make a SuperviewController class and define the same function on it.

class SuperViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
            // Do any additional setup after loading the view.
    }
    @objc func buttonClicked(sender: UIBarButtonItem) {

    }
}

and In our base viewController(which inherit your SuperviewController class) override the same function

import UIKit

class HomeViewController: SuperViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        GeneralUtility.customeNavigationBar(viewController: self,title:"Event");
    }

    @objc override func buttonClicked(sender: UIBarButtonItem) {
      print("button clicked")    
    } 
}

Now just inherit the SuperViewController in whichever class you want this barbutton.

Thanks for the read

0

Swift 5

if you have created UIBarButtonItem in Interface Builder and you connected outlet to item and want to bind selector programmatically.

Don't forget to set target and selector.

addAppointmentButton.action = #selector(moveToAddAppointment)
addAppointmentButton.target = self

@objc private func moveToAddAppointment() {
     self.presenter.goToCreateNewAppointment()
}
0

If you don't want to use target/action using a #selector, you can use primaryAction since iOS 14:

// here you can set the title or a image for the UIBarButtonItem
let action = UIAction(title: "", image: UIImage(systemName: "list.dash")) { _ in
    // handler
    print("hello world")
}
        
let barButtonItem = UIBarButtonItem(title: nil,
                                    image: nil,
                                    primaryAction: action,
                                    menu: nil)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.