@IBAction在Swift中崩溃了应用程序

时间:2014-06-15 06:56:57

标签: ios swift

我有按钮和方法

 switchViewButton = UIButton.buttonWithType(.System) as? UIButton
    switchViewButton!.frame = CGRectMake(15, 25, 50, 50)
    switchViewButton!.setTitle("sss", forState: .Normal)
    switchViewButton!.addTarget(self, action: "switchViewFunc", forControlEvents: .TouchUpInside)
    switchViewButton!.backgroundColor = UIColor.redColor()
    switchViewButton!.layer.cornerRadius = 25
    topView.addSubview(switchViewButton)


@IBAction func switchViewFunc(sender : AnyObject){
     println("Button was clicked", sender)}

此方法使应用程序崩溃,原因是“无法识别的选择器已发送到实例”

我有另一个按钮和方法就像这样,它的工作非常完美。如果我删除(sender : AnyObject)它可以正常工作。我无法理解什么是错的

3 个答案:

答案 0 :(得分:4)

以这种方式添加动作(注意“:”

switchViewButton!.addTarget(self, action: "switchViewFunc:", forControlEvents: .TouchUpInside)

答案 1 :(得分:1)

@IBAction func switchViewFunc(sender : AnyObject){
     println("Button was clicked", sender)} - ?????

试试这个:

func switchViewFunc(sender : UIButton!){
     println("Button was clicked", sender)
}

答案 2 :(得分:1)

不需要@IBAction和参数。

func switchViewFunc(){
    println("Button was clicked", sender)
}