Alexandra Vorobyova
Alexandra Vorobyova

Reputation: 107

UIButton in Swift - action is not working

I'm writing my first IOs app. I have my class animation where I register an action for my button

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:]

Can you help me please what am I doing wrong?

Upvotes: 0

Views: 2773

Answers (1)

Sandy Chapman
Sandy Chapman

Reputation: 11341

You're setting the button's action to target the animation object where the actual method is defined in your ViewController.

You should pass the ViewController in to the animation's init class and use it as the target:

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

Upvotes: 5

Related Questions