将数据从ViewController传递到ContainerView

时间:2019-03-28 14:51:01

标签: ios swift

我正在使用网络请求从ViewController的后端检索数据,并且此视图包含三个容器,因此,我想将这些数据传递到容器中。在我使用segue for segue时失败了。

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

    if segue.identifier == "ContinueSurvey" {
        let continueSurveyVC = segue.destination as! ContinueSurveyVC
        continueSurveyVC.notComplatedSurveies = notComplatedSurveies
    } else if segue.identifier == "LatestOffers" {
        let latestOffersVC = segue.destination as! LatestOffersVC
        latestOffersVC.latestOffers = latestOffers
    } else if segue.identifier == "LatestSurvey" {
        let latestSurveyVC = segue.destination as! LatestSurveyVC
        latestSurveyVC.latestSurveies = latestSurveies
    }
}

3 个答案:

答案 0 :(得分:0)

这可能会有所帮助:https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

  

这是一个视图控制器MainViewController,其属性为   文字:

src
  

无论何时创建MainViewController实例,都可以分配   文本属性的值。像这样:

class MainViewController: UIViewController
{
    var text:String = ""

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
}
  

这是视图控制器的代码:

let vc = MainViewController()
vc.text = "Hammock lomo literally microdosing street art pour-over"
  

然后,这是数据的实际传递…添加以下方法   到MainViewController:

class SecondaryViewController: UIViewController
{
    var text:String = ""

    @IBOutlet weak var textLabel:UILabel?

    override func viewDidLoad()
    {
        super.viewDidLoad()

        textLabel?.text = text
    }
}

答案 1 :(得分:0)

您可以使用当前的ViewController并在viewWillAppear的当前的ViewController中检查发生了什么:

提供VeiwController:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "yourViewControllerIdentifire") as! yourViewController
self.present(vc, animated: true, completion: nil)
您的ViewContriller中的

&使用viewWillAppear将这些数据传递到容器中:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
   //pass those data into containers
}

答案 2 :(得分:0)

如果要在子控制器已加载时对其进行更新,则需要在其上将引用存储在父控制器中,然后在需要的时候更新数据,如下所示:

weak var continueSurveyVC: ContinueSurveyVC?
weak var latestOffersVC: LatestOffersVC?
weak var latestSurveyVC: LatestSurveyVC?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ContinueSurvey" {
        continueSurveyVC = segue.destination as! ContinueSurveyVC
        continueSurveyVC?.notComplatedSurveies = notComplatedSurveies
    } else if segue.identifier == "LatestOffers" {
        latestOffersVC = segue.destination as! LatestOffersVC
        latestOffersVC?.latestOffers = latestOffers
    } else if segue.identifier == "LatestSurvey" {
        latestSurveyVC = segue.destination as! LatestSurveyVC
        latestSurveyVC?.latestSurveies = latestSurveies
    }
}

/// Call this method anytime you want to update children data
func updateChildData() {
    continueSurveyVC?.notComplatedSurveies = notComplatedSurveies
    latestOffersVC?.latestOffers = latestOffers
    latestSurveyVC?.latestSurveies = latestSurveies
}