简体   繁体   English

UIToolBar上的UIButton似乎不起作用

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

Here's my code 这是我的代码

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)
}

method barItem2Clicked : 方法barItem2Clicked

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

I wanted to use UIButton add to UIToolBar, but the action of button1 can not be called, and the Highlighted effect is not shown too. 我想使用UIButton添加到UIToolBar,但是无法调用button1的动作,也不会显示“突出显示”效果。

I clicked the button1 I just added, but the method barItem2Clicked never called. 我单击了刚刚添加的button1 ,但是从未调用过barItem2Clicked方法。

Did I missed something? 我错过了什么吗?

Thanks 谢谢

Please make sure that the toolBar1 is connected I just tried with your code and it seems to be working. 请确保toolBar1已连接,我刚刚尝试过使用您的代码,并且似乎可以正常工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM