2017-06-09 4 views
0

私はちょうどすぐにプロトコル/デリゲートの学習を開始し、thisの質問を修正しようとしましたが失敗しました(ipad air2、iOS10.2、xcode8.2.1、swift3) メインviewControllerのボタンが押されたときのコンテナのラベル。何も起こりません。コンテナとviewControllerの間でデリゲートを設定する

enter image description here main viewControllerのコード。

import UIKit 

class ViewController: UIViewController { 

    var dataViewDelegate: DataViewDelegate? 


    @IBOutlet weak var container: UIView! 


    @IBAction func touchMe(_ sender: Any) { 

     dataViewDelegate?.setTouch(touch: true) 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 


} 

とコンテナのため、

protocol DataViewDelegate { 
    func setTouch(touch:Bool) 
} 



import UIKit 

class ContainerViewController: UIViewController, DataViewDelegate { 

    var dataViewDelegate: DataViewDelegate? 



    @IBOutlet weak var labelText: UILabel! 



    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     if(segue.identifier == "identifier"){ 

      let embedVC = segue.destination as! ViewController 
      embedVC.dataViewDelegate = self 
     } 
    } 


    func setTouch(touch: Bool) { 
     if touch == true { 
      labelText.text = "Touched!" 
     } 
    } 


} 

答えて

0

ここでは、だけではなく、あなたがこの方法のために持っているものをお使いのViewControllerに

@IBAction func touchMe(_ sender: Any) { 
     container.labeltext.text = "your text" 
    } 

を追加し、デリゲートメソッドを使用する必要はありませんが、以下のように。

それはそれです、それはそれよりも難しくしないでください。

あなたのViewControllerクラスでContainerViewController

からあなたのViewControllerでアクションがあなたのContainerViewControllerの代理人であることを、それを設定するトリガーに何かを持っていた場合は、デリゲートを使用することができます。デリゲートが誰であるかを知る必要があります。次に、あなたのviewDidLoadに何かを追加してください。

containerViewController.delegate = self 
+0

私は試しましたが、私はまだ理解していません。私は新しい質問を投稿しました。助言してください。[link](https://stackoverflow.com/questions/44491105/change-text-in-container-from-main-viewcontroller) – sandalwalk

+0

あなたは何を理解していませんか?私は説明したいと思いますか?私が答えたものがあなたのテキストをラベルに表示するのを助けたら、あなたは私の答えに目を向けることができます。 – Bliss

関連する問題