I have a MainViewController embed in a Navigation Controller, as shown below:
And in MainViewController.swift, I added two UIBarButtonItem(left and right) programmatically:
class MainViewController: UIViewController {
let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(onRightClick))
let leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(onLeftClick))
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = rightButton
navigationItem.leftBarButtonItem = leftButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@objc func onRightClick() {
print("[Main] Right Click")
}
@objc func onLeftClick() {
print("[Main] Left Click")
}
}
The buttons did show on the screen, but the interesting thing is, the selector functions onLeftClick
and onRightClick
never get called whenever I pressed left or right button. Is there anything I should do to make it works? I am using Xcode 9.3.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…