UIButton不会打电话给@IBAction

时间:2014-09-03 10:12:52

标签: ios swift uibutton

我是swift的新手,在点击UIButton时尝试调用loginAuth方法。我做的与我在Objective-c项目中所做的完全相同,但我一直在跟踪错误:

[SwiftSwipeView.LoginViewController loginAuth]: unrecognized selector sent to instance

的UIButton

var logIn: UIButton!

viewDidLoad中

    logIn = UIButton(frame: CGRect(x: (self.view.bounds.width/2)-(245/2), y: self.view.bounds.height-200, width: 245.00, height: 37.50));
    logIn.backgroundColor = UIColor(red:0.29, green:0.345, blue:0.4, alpha:1)
    logIn.setTitle("LOG IN", forState: UIControlState.Normal)

    logIn.titleLabel?.font =  UIFont(name: "Lato-Bold", size: 14)
    logIn.layer.cornerRadius = 5;
    logIn.clipsToBounds = true;
    logIn.addTarget(self, action: "loginAuth", forControlEvents: .TouchUpInside)
    self.view.addSubview(logIn)

LoginAuth方法

@IBAction func loginAuth(sender: UIButton!) {


    println("test")

}

3 个答案:

答案 0 :(得分:2)

您应该在实施时声明目标行动。

改变这个:

logIn.addTarget(self, action: "loginAuth", forControlEvents: .TouchUpInside)

logIn.addTarget(self, action: "loginAuth:", forControlEvents: .TouchUpInside)

此消息unrecognized selector sent to instance始终表示未尝试执行的方法...

答案 1 :(得分:0)

试试这个  (在选择器名称后添加冒号)

logIn.addTarget(self, action: "loginAuth:", forControlEvents: .TouchUpInside)

答案 2 :(得分:0)

试试这个:

logIn = UIButton(frame: CGRect(x: (self.view.bounds.width/2)-(245/2), y: self.view.bounds.height-200, width: 245.00, height: 37.50));
logIn.backgroundColor = UIColor(red:0.29, green:0.345, blue:0.4, alpha:1)
logIn.setTitle("LOG IN", forState: UIControlState.Normal)

logIn.titleLabel?.font =  UIFont(name: "Lato-Bold", size: 14)
logIn.layer.cornerRadius = 5;
logIn.clipsToBounds = true;
logIn.addTarget(self, action: "loginAuth:", forControlEvents: .TouchUpInside)
self.view.addSubview(logIn)

正如您的loginAuth期待参数。