[英]#selector function of UIBarButtonItem not firing when pressed
我創建了此右欄按鈕項,但是當按下時什么也沒有發生。 #selector沒有觸發,我知道@objc函數有效。
let button = UIButton(type: .custom)
button.setTitle("Uncheck All", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
button.setTitleColor(.bandzBlue(), for: .normal)
button.layer.cornerRadius = 5
button.backgroundColor = .white
button.frame = CGRect(x: 0, y: 0, width: 92, height: 14)
button.addTarget(self, action: #selector(self.addTapped), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
@objc func addTapped() {
stuff that works fine
}
也許實例的其余部分在代碼執行時尚未初始化。 您是否嘗試過在viewDidLoad
執行此操作? 如果沒有,請嘗試這樣做:
override func viewDidLoad() {
super.viewDidLoad()
configureYourBarButtonHere()
}
我記得在此解決方案中,有很多問題突出了這個問題。 這是我現在可以找到的幾個:
嘗試
button.setTitleColor(UIColor.gray, for: .normal)
代替
button.setTitleColor(.bandzBlue(), for: .normal)
和UIButton(type: .system)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.