Pass data between ViewController and ContainerViewController

隐身守侯 提交于 2019-11-27 14:22:06

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
    }
}
Rob

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!