简体   繁体   English

Swift中的UIButton-操作不起作用

[英]UIButton in Swift - action is not working

I'm writing my first IOs app. 我正在编写我的第一个IO应用程序。 I have my class animation where I register an action for my button 我有课堂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')
      }
}

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

Can you help me please what am I doing wrong? 您能帮我做错什么吗?

You're setting the button's action to target the animation object where the actual method is defined in your ViewController . 您正在将按钮的动作设置为定位到在ViewController定义了实际方法的animation对象。

You should pass the ViewController in to the animation's init class and use it as the target: 您应该将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) 
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM