aircraft aircraft - 1 month ago 9
iOS Question

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

Create a

UIBarButtonItem
which name is
nextVc
on my
navigationBar
, 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() {
super.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

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.