简体   繁体   中英

IOS Swift: Button Action in a separate class file

I just need to implement a custom class that takes a reference of a button in initialization and then perform the action (Touch Up Inside) in there.

Just tried the folloiwng way but getting an exception saying 'unrecognized selector sent to instance 0x7fbb48615ac0'

import UIKit
class CustomButton
{
    var view: UIViewController
    var btnTest: UIButton!

    init(view: UIViewController, testBtn: UIButton)
    {
        self.view = view
        self.btnTest = testBtn

        // self.btnTest.addTarget(self, action: "btnMethod:", forControlEvents: UIControlEvents.TouchUpInside)
        self.btnTest.addTarget(self, action: Selector("btnMethod:"), forControlEvents: UIControlEvents.TouchUpInside)
    }

    func btnMethod(sender: UIButton)
    {
        println("Click alright.....")
    }
}

The complete exception message is as follows

在此处输入图片说明

Can't really get the point where I am going wrong in here. Would be grateful to hear what I am doing wrong. Thanks in advance!!!

Edits

Following is the full working solution

class CustomButton: NSObject
{
   var view: UIViewController
   var btnTest: UIButton!

   init(view: UIViewController, testBtn: UIButton)
   {
        self.view = view
        self.btnTest = testBtn

        super.init()
        self.btnTest.addTarget(self, action: Selector("btnMethod:"), forControlEvents: UIControlEvents.TouchUpInside)
    }

    deinit
    {
        println("De-Init happens...")
    }

    func btnMethod(sender: UIButton)
    {
        println("Click alright.....")
    }
}

如果您更正确地看到自己的错误...由于未捕获的异常而正在为应用程序加注[NSArray I按钮方法:]。因此您的选择器是错误的。

self.btnTest.addTarget(self, action:"btnMethod:", forControlEvents: UIControlEvents.TouchUpInside)

The code in the CustomButton class is straightforward and looks correct. The problem is that the class instance (variable) is being deinitialized and then the button is being tapped and it's trying to call the selector on an uninitialized variable.

You can confirm this by adding to your CustomButton class:

deinit {
   println("deinit")
}

If you see the deinit happen before you tap the button, you know it's going to crash when you tap.

The solution is to make sure your CustomButton variable (customBtn) in the UIViewController is a property of the class and not a local variable. That way the CustomButton will stick around as long as your UIViewController does.

class PaymentViewController: UIViewController {

  var customButton: CustomButton!
  let button: UIButton!

  override func viewDidLoad() {
     super.viewDidLoad()

     // initialize self.button

     // this instance will stick around for the life of the PaymentViewController
     customButton = CustomButton(self, button)
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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