将数据从 ViewController 转发到 ContainerView

Posted

技术标签:

【中文标题】将数据从 ViewController 转发到 ContainerView【英文标题】:Passing data forward from ViewController to ContainerView 【发布时间】:2019-03-28 14:51:01 【问题描述】:

我正在使用网络请求从 ViewController 的后端检索数据,这个视图包含三个容器,所以我想将这些数据传递到容器中。在我使用 prepare 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
    

【问题讨论】:

添加有问题的代码将帮助我们更好地帮助您。 你能展示你的代码吗? 我认为这是一种不同的情况,因为我调用 API 请求所以需要时间来响应,所以视图在获取数据之前移动到容器视图。 @ManWithBear @BolaIbrahim 你说得对,对不起 【参考方案1】:

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

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

class MainViewController: UIViewController

    var text:String = ""

    override func viewDidLoad()
    
        super.viewDidLoad()
    

每当您创建 MainViewController 的实例时,您都可以分配 text 属性的值。像这样:

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

这是视图控制器的代码:

class SecondaryViewController: UIViewController

    var text:String = ""

    @IBOutlet weak var textLabel:UILabel?

    override func viewDidLoad()
    
        super.viewDidLoad()

        textLabel?.text = text
    

那么,这里是数据的实际传递... 添加下面的方法 到 MainViewController:

@IBAction func onButtonTap()

    let vc = SecondaryViewController(nibName: "SecondaryViewController", bundle: nil)
    vc.text = "Next level blog photo booth, tousled authentic tote bag kogi"

    navigationController?.pushViewController(vc, animated: true)

【讨论】:

【参考方案2】:

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

现在的 VeiwController:

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

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

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

【讨论】:

【参考方案3】:

如果您想在子控制器已经加载时更新它们,您需要将它们的引用存储在父控制器中,然后在需要的时刻更新数据:

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

【讨论】:

以上是关于将数据从 ViewController 转发到 ContainerView的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从模态 ViewController 传输到父 ViewController?

将数据从 TableView 传递到 ViewController

将数据从 UITableViewCell 传递到 ViewController

将数据从 ViewController 传递到 TabViewController

将数据从 ViewController 传递到 TabBarController

将数据从 ViewController 传递到 NSObject