簡體   English   中英

UIToolBar上的UIButton似乎不起作用

[英]The UIButton on UIToolBar seems to be not working

這是我的代碼

override func viewDidLoad() {
    super.viewDidLoad()

    let button1 = UIButton(frame: CGRect(x: 50, y: 50, width: 30, height: 30))
    button1.setTitle("hi", for: .normal)
    button1.setTitleColor(.blue, for: .normal)
    button1.setTitleColor(.black, for: .highlighted)
    button1.addTarget(self, action: #selector(self.barItem2Clicked(sender:)), for: .touchUpInside)
    button1.backgroundColor=UIColor.red

    let barButton1 = UIBarButtonItem(customView: button1)
    toolBar1.items?.append(barButton1)
}

方法barItem2Clicked

func barItem2Clicked(sender :Any?) {
    NSLog("hello")
}

我想使用UIButton添加到UIToolBar,但是無法調用button1的動作,也不會顯示“突出顯示”效果。

我單擊了剛剛添加的button1 ,但是從未調用過barItem2Clicked方法。

我錯過了什么嗎?

謝謝

請確保toolBar1已連接,我剛剛嘗試過使用您的代碼,並且似乎可以正常工作。

在此處輸入圖片說明

暫無
暫無

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

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