Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift:class does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector

Tags:

ios

swift

class FirstView: UIView {           
        @IBOutlet weak var lbl1: UILabel!
            @IBOutlet weak var lbl1: UILabe2!
            @IBOutlet weak var btn1: UIButton!
            override init(frame: CGRect) {
                super.init(frame: frame)
                setupView()
            }

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

            func setupView() {
           //view setups

        }
    }

Now in next class i am trying to get the action event of the button but it always charshes saying class 'App. UIViewHelper' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector +[App. UIViewHelper NotesClicked:]

class UIViewHelper
    {

     case 1:
       let lowerDetail = FirstView()
            lowerDetail.lbl1.text = "Worked"
            lowerDetail.lblw.text = "Happy"
              lowerDetail.btn1.addTarget(self, action:   #selector(NotesClicked), forControlEvents: UIControlEvents.TouchUpInside)

            print("Sleep")
            break

       }
      @objc func NotesClicked(sender :UIButton) {

            print("Worked")
    }
    }

and when UIViewHelper extends NSObject is crashes saying * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[App.UIViewHelper NotesClicked:]: unrecognized selector sent to class 0x105771a80'

I am confused what is happening here.Can anybody suggest this??

like image 947
Sam Avatar asked Jun 16 '17 07:06

Sam


2 Answers

I think that your problem is that you do not subclass NSObject from UIViewHelper, try:

class UIViewHelper : NSObject{
like image 178
Carles Estevadeordal Avatar answered Sep 29 '22 18:09

Carles Estevadeordal


You trying to add a selector from a static function to an instance of UIViewHelper.

When you are saying

lowerDetail.btn1.addTarget(self, action:   #selector(NotesClicked), forControlEvents: UIControlEvents.TouchUpInside)

You are adding a target action to self (an instance of UIViewHelper), but, from a static function.

I suppose you do not create an instance of UIViewHelper, like you mentioned in the comment that the switch case is called from a static function, and therefore the target is referencing a nil object.

To fix the code, you need to keep a reference to UIViewHelper, and not call it from a static function.

How to do it? Here is a short snippet to give you an idea in Swift 4.

class ViewController: UIViewController {

    let uiHelper = UIViewHelper()

    override func viewDidLoad() {
        super.viewDidLoad()
        let some = uiHelper.makeView()
        self.view.addSubview(some)
    }
}

class UIViewHelper {
  func makeView() -> FirstView {
        let lowerDetail = FirstView(frame: CGRect(origin: .zero, size: CGSize(width: 39, height: 39)))
        lowerDetail.lbl1.text = "Worked"
        lowerDetail.lbl2.text = "Happy"
        lowerDetail.btn1.addTarget(self, action: #selector(UIViewHelper.NotesClicked), for: .touchUpInside)

        return lowerDetail
    }

    @objc func NotesClicked(sender :UIButton) {
        print("Worked")
    }
}
like image 33
dirtydanee Avatar answered Sep 29 '22 18:09

dirtydanee