2016-09-20 9 views
0

**以下に記載されている内容はすべてプログラムによって実行されています。 StoryBoardを使用しない**Swift:デリゲートの有無にかかわらず "インスタンスに送信されたセレクタが認識されません"

私のViewControllerでは、ボタンとラベルを含むCustomCheckBoxContainerを作成します。 (ここではラベルは表示されません!!)私はボタンにアクションを追加しました。そして、子供の中の機能(CustomCheckBoxContainerやCustomCheckBoxなど)を扱っている限り、湿気は明るくて明るいです。しかし、ViewControllerで関数を呼び出すアクションを追加しようとすると、クラウドは私のプロジェクトの上に形成され始めます。

エラーメッセージ:***キャッチされていない例外 'NSInvalidArgumentException'のため、アプリケーションを終了します。理由: ' - [UIView buttonClicked3:]:インスタンスに送信された認識できないセレクタ...'。

ボタンアクション:

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

は、その後、あなたが戻って上流のデータやアクションを送ったことはできません実現しました。作成されたプロトコルとデリゲートし、アクションを書き換えます。

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

残念ながら、まったく同じ結果です。

私は完全に固執しています。私は何を見ていないのですか?

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") 
    } 
} 

答えて

0

私は2つの段階に分割します。

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

その後buttonTapped(の実装内からデリゲートメソッドを呼び出す_ :):

リンク子ビューコントローラ内のセレクタへのボタン次の行に沿って(次は2.3構文スウィフトです) :

func buttonTapped() -> Void { 
    Self.delegate.buttonWasTapped() 
    ... 
} 

[なお、デリゲートがnilでないことも確認してください。暗黙のうちにアンラップされたオプションであると宣言しましたが、親ビューコントローラからデリゲートを設定する必要があります。]

+0

これは素晴らしいヒントでした。もっと読みやすいと思う。だから私はそれを示唆して実装しました。結局のところ、ソリューションは、あなたが予測したように、デリゲートが実際には「無」であるように見えました。私はViewControllerで値を割り当てることを見落としました。問題を解決するには、次の文をviewDidLoadに追加してください: "CheckBoxContainer.delegate = self"。 – RempyAce