【问题标题】:Why is my UIButton's target never reached?为什么我的 UIButton 的目标从未达到?
【发布时间】:2014-10-28 22:48:51
【问题描述】:
    func setupNewBinderButton() {
    let binderButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    binderButton.frame = CGRectMake(50, 384-25, 50, 50)
    binderButton.setTitle("Add", forState: UIControlState.Normal)
    binderButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    binderButton.addTarget(self, action: "addBinderTapped", forControlEvents: UIControlEvents.TouchUpInside)
    binderButton.userInteractionEnabled = true
    self.view.addSubview(binderButton)
    }



func addBinderTapped() {
    println("hi")
    abort()
}




override func viewDidLoad() {
super.viewDidLoad()

setupNewBinderButton()

我在这个函数中设置的断点没有被命中。日志没有被击中。中止不会发生。

如何让我的 UIButton 在 Swift 中响应触摸?

【问题讨论】:

  • 没有骰子。我尝试使用 action:Selector("addBinderTapped:") 和 @objc func addBinderTapped(button:UIButton!) { 作为链接引用。
  • 所以在第二个例子中,我必须添加代码只是为了获得一个按钮来注册一个功能?必须有一种内置方法。
  • 很遗憾目前没有内置方法...可能需要等待下一个 SDK 版本

标签: swift uibutton


【解决方案1】:

这样使用,因为它需要绑定到 IBAction

@IBAction func addBinderTapped(sender: UIButton)
{
}

通过命令+拖动到情节提要按钮将此操作与您的按钮连接以绑定此功能。

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多