aircraft aircraft - 1 year ago 135
iOS Question

How to add action to UIBarButtonItem which is created by storyboard in swift?

Create a

which name is
on my
, and set its action by
nextVc.action = #selector(self.gotoVC4)
, but it does not work.

my code is below:

class ViewController3: UIViewController {

@IBOutlet weak var nextVc: UIBarButtonItem!

override func viewDidLoad() {

nextVc.action = #selector(self.gotoVC4)


func gotoVC4() -> Void {

print("go to vc4")

let vc4 = ViewController4()
self.navigationController!.pushViewController(vc4, animated: true)



and the image of storyboard is here:

the storyboard shootscreen

Answer Source

Since you have a storyboard, simply ctrl-drag from the bar button to the "View Controller 4" scene to create a segue. No code needed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download