Pass data between ViewController and ContainerViewController

后端 未结 2 1781
别跟我提以往
别跟我提以往 2020-12-03 05:37

I\'m working on an app, and need to pass data between view and containerView. I need to send data and receive data from both Views.

Let me explain better:

I

相关标签:
2条回答
  • 2020-12-03 05:49

    All you need to do is keep a reference to Container in your master view controller.

    That is, you should add an instance variable to Master that will hold a reference to the view controller, not just the view. You'll need to set it in prepareForSegue.

    So the beginning of Master View Controller would look something like this:

    class Master: UIViewController, ContainerToMaster {
    
    @IBOutlet var containerView: UIView!
    
    var containerViewController: Container?
    
    @IBOutlet var labelMaster: UILabel!
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "containerViewSegue" {
            containerViewController = segue.destinationViewController as? Container
            containerViewController!.containerToMaster = self
        }
    }
    

    And then in your button function, simply change the label using the variable you just added.

    Example:

    @IBAction func button_Container(sender: AnyObject) {
        containerViewController?.changeLabel("Nice! It's work!")
    }
    

    This means you can get rid of your MasterToContainer protocol too.

    I tested this code, so I know it works, but unfortunately I am an Objective-C dev, and know nothing about best practices in Swift. So I don't know if this is the best way to go about it, but it certainly works.

    Edit:

    Here's the exact code I've tested:

    Master.swift:

    import UIKit
    
    class Master: UIViewController, ContainerToMaster {
    
        @IBOutlet var containerView: UIView!
        @IBOutlet var labelMaster: UILabel!
        var containerViewController: Container?
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "containerViewSegue" {
                containerViewController = segue.destinationViewController as? Container
                containerViewController!.containerToMaster = self
            }
        }
    
        @IBAction func button_Container(sender: AnyObject) {
            containerViewController?.changeLabel("Nice! It's work!")
        }
    
        func changeLabel(text: String) {
            labelMaster.text = text
        }
    }
    

    Container.swift:

    import UIKit
    
    protocol ContainerToMaster {
        func changeLabel(text:String)
    }
    
    class Container: UIViewController {
    
        @IBOutlet var labelContainer: UILabel!
        var containerToMaster:ContainerToMaster?
    
        @IBAction func button_Master(sender: AnyObject) {
            containerToMaster?.changeLabel("Amazing! It's work!")
        }
    
        func changeLabel(text: String) {
            labelContainer.text = text
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:49

    I solved it with this code

    To send data from ViewController -> ContainerViewController

    Class ViewController : UIViewController {
    
    func sendData(MyStringToSend : String) {
    
    let CVC = childViewControllers.last as! ContainerViewController
    CVC.ChangeLabel( MyStringToSend)
    }
    
    }
    

    in your ContainerViewController

    Class ContainerViewController : UIViewController {
    
    @IBOutlet weak var myLabel: UILabel!
    
    func ChangeLabel(labelToChange : String){
    
    myLabel.text = labelToChange
    
     }
    }
    

    To send data from ContainerViewController -> ViewController

    Class ContainerViewController : UIViewController {
    
    func sendDataToVc(myString : String) {
    
       let Vc = parentViewController as! ViewController
       Vc.dataFromContainer(myString)
     }
    
    }
    

    and in ViewController

      Class ViewController : UIViewController {
    
      func dataFromContainer(containerData : String){
    
       print(containerData)
      }
    
      }
    

    I hope this will help someone.

    0 讨论(0)
提交回复
热议问题