Swift: "unrecognised selector sent to instance" both with/without delegate

时间:2016-09-20 21:04:12

标签: swift delegates

** Everything described below was done programmatically. No use of StoryBoard **

In my ViewController I create a CustomCheckBoxContainer that contains buttons and labels. (labels not shown here!!) I've added actions to the buttons and as long as I address a function in a child (eg. CustomCheckBoxContainer and CustomCheckBox) the wether is shiny and bright. However, when trying to add an action that calls a function in ViewController, clouds start to form above my project.

Error message: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView buttonClicked3:]: unrecognized selector sent to instance ...'.

Button action:

newButton.addTarget(currentView, action: #selector(ViewController.buttonClicked3(_:)), forControlEvents: UIControlEvents.TouchUpInside)

Then realised you can't sent data or actions back upstream. So created protocol and delegate and rewritten the action to:

newButton.addTarget(self, action: #selector(delegate3.buttonClicked3(_:)), forControlEvents: UIControlEvents.TouchUpInside)

. Unfortunately, exact same result.

I'm completely stuck. What am I not seeing?

ViewController.swift

class ViewController: UIViewController, ButtonClicked3Delegate  {

 @objc func buttonClicked3(sender: UIButton) {
       print ("Action received in the ViewController Class")
    }

let userArray: [String] = ["one","two”]

override func viewDidLoad() {
    super.viewDidLoad()

    for item in userArray {
        let customCheckBoxContainer = CustomCheckBoxContainer()
        customCheckBoxContainer.showNewButton(..args.., currentView: UIView, ... args ...)
        self.view.addSubview(customCheckBoxContainer)
    }
  }
}

CustomCheckBoxContainer.swift:

    @objc protocol ButtonClicked3Delegate {
    @objc func buttonClicked3(sender: UIButton)
    //other data needed
}

class CustomCheckBoxContainer: UIView {
    var newButton: CustomCheckBox!

    var delegate3 : ButtonClicked3Delegate!

    func showNewButton (... args...  currentView: UIView, ... args ... ) {
        newButton = CustomCheckBox (type: UIButtonType.Custom)
        newButton.bounds = ...
        newButton.center = ...

        newButton.addTarget(newButton, action: #selector(CustomCheckBox.buttonClicked1(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        newButton.addTarget(self, action:     #selector(CustomCheckBoxContainer.buttonClicked2(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        newButton.addTarget(self, action:     #selector(delegate3.buttonClicked3(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        currentView.addSubview(newButton)

    }

    func buttonClicked2(sender:UIButton) {
        print ("Action received in the CustomCheckBoxContainer Class")
    }
}

CustomCheckBox.swift

class CustomCheckBox: UIButton {

    func buttonClicked1(sender:UIButton) {
        print ("Action was received in CustomCheckBox Class")
    }
}

1 个答案:

答案 0 :(得分:0)

我会把它分成两个阶段。

将按钮链接到子视图控制器中的选择器(以下是Swift 2.3语法),大致如下:

button.addTarget(self, action #selector(self.buttonTapped()), forControlEvents: UIControlEvents.touchUpInside)

然后在buttonTapped(_:):

的实现中调用委托方法
func buttonTapped() -> Void {
    Self.delegate.buttonWasTapped()
    ...
}

[顺便说一下,还要检查代表是否为零。您已将其声明为隐式解包的可选项,但这意味着您需要从父视图控制器设置委托。]