簡體   English   中英

按下時不會觸發UIBarButtonItem的#selector函數

[英]#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()
}

我記得在此解決方案中,有很多問題突出了這個問題。 這是我現在可以找到的幾個:

UIBarButtonItem選擇器不起作用

https://stackoverflow.com/a/49283627/9293498

嘗試

 button.setTitleColor(UIColor.gray, for: .normal)

代替

button.setTitleColor(.bandzBlue(), for: .normal)

UIButton(type: .system)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM