【问题标题】:UIButton wont call @IBActionUIButton 不会调用@IBAction
【发布时间】:2014-09-03 10:12:51
【问题描述】:

我是 swift 的新手,并尝试在点击 UIButton 时调用 loginAuth 方法。我所做的与我在 Objective-C 项目中所做的完全一样,但我不断收到以下错误:

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

UI按钮

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)

登录验证方法

@IBAction func loginAuth(sender: UIButton!) {


    println("test")

}

【问题讨论】:

  • 似乎你在一个项目中混合了objective-c和swift。错误消息看起来像 ObjC,你的 ode 是 Swift。 Swift 错误消息看起来不同。
  • 嗯,当我点击 loginAuth 按钮时,错误就会出现,所以我猜错误应该在 loginAuth 方法和 logIn 按钮之间?

标签: ios swift uibutton


【解决方案1】:

您应该在实施时声明目标操作。

改变这个:

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

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

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

【讨论】:

    【解决方案2】:

    试试这个:

    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 需要一个参数。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-18
        • 1970-01-01
        相关资源
        最近更新 更多