我有按钮和方法

 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),则可以使用。我不明白怎么了

最佳答案

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

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

10-08 07:27