swift - UIButton 创建错误 - "unrecognized selector sent to instance"

标签 swift uibutton

我已经使用这段代码在我的 View 中添加了一个按钮,但是它出现了错误“无法识别的选择器发送到实例”并使应用程序崩溃。

有人知道这是为什么吗?

非常感谢!

       let button: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
        button.backgroundColor = UIColor.clearColor()
        button.setTitle("go Back", forState: UIControlState.Normal)
        button.addTarget(self, action:  "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)


    }

    func buttonAction(sender: UIButton!) {

        println("SegueSTGB")
        performSegueWithIdentifier("SegueSTGB", sender: self)

    }

最佳答案

你应该修改addTarget:

button.addTarget(self, action:Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)

关于swift - UIButton 创建错误 - "unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651112/

相关文章:

ios - iOS中如何实现长振动

ios - 如何给 UIButton 标签赋负值?

ios - 使标题成为 UIButton 中可点击区域的一部分

ios - 通过遍历字典显示随机问题

arrays - 尝试保存到 NSUserdefaults 时出错

ios - 无法以正确的格式获取 XML 数据以在 Swift 的 TableView 中显示

ios - 如何将我的 UIImageView 内容保存到我的画廊? ( swift )

swift - 将 Subview 置于最前面会弄乱 UIView.animate

ios - 如何检测 UIButton 背景图片的变化?

ios - 不同屏幕尺寸的用户界面图像大小?