Swift中的UIButton - 行动无效

Ale*_*ova 0 ios swift

我正在编写我的第一个IO应用程序.我有我的班级animation,我为我的按钮注册了一个动作

class animation {
    var view: UIView!
    init(var viewLink:UIView) {
      self.view = viewLink
      let buttonNext   = UIButton.buttonWithType(UIButtonType.System) as UIButton
      buttonNext.frame = CGRectMake(200, 250, 100, 50)
      buttonNext.addTarget(self, action: "nextActionButton:", forControlEvents: UIControlEvents.TouchUpInside)
      self.view.addSubview(buttonNext)
    }
}
class ViewController: UIViewController {
      private var animClass: animation?

      required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
      }

      override func viewDidLoad() {
         super.viewDidLoad()
         animClass = animation(viewLink: self.view) 
      }

      func nextActionButton(sender:UIButton!) {
         println('rrr')
      }
}
Run Code Online (Sandbox Code Playgroud)

我有一个错误: NSForwarding: warning: object 0x7fd820c94c00 of class 'animation.animation' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[animation.animation nextActionButton:]

你能帮帮我吗,我做错了什么?

San*_*man 5

您正在设置按钮的操作以定位animation在您的实际方法中定义的对象ViewController.

您应该将ViewController传递给动画的init类并将其用作目标:

init(var viewLink:UIView, eventTarget : AnyObject) {
    ...
    buttonNext.addTarget(eventTarget, action: "nextActionButton:", forControlEvents: UIControlEvents.TouchUpInside)
    ...
}
...
override func viewDidLoad() {
    ...
    animClass = animation(viewLink: self.view, eventTarget:self) 
    ...
}
Run Code Online (Sandbox Code Playgroud)