Passing Data to view controllers that are embedded in container views

后端 未结 1 1067
夕颜
夕颜 2020-12-09 23:13

I have view controllers that just need to get passed a NSDictionary called \"otherUser\". I am using a segmented controller to conveniently present 4 of these views to a use

相关标签:
1条回答
  • 2020-12-10 00:00

    In your Storyboard, when you embed a VC in a ContainerView, you also see a "segue" connecter. When the root VC loads, you will get a call to prepare for segue for that.

    Give each storyboard-created segue an Identifier - such as "infoViewEmbedSegue", "feedViewEmbedSegue", etc.

    In your root VC, I'm guessing that

    var infos: BusinessProfilesDetails!
    var feeds: BusinessProfilePostsFeed!
    

    are variables to reference the content of infoView? If so, in prepare() you want to:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        // get a reference to the embedded PageViewController on load
    
        if let vc = segue.destination as? BusinessProfilesDetails,
            segue.identifier == "infoViewEmbedSegue" {
            self.infos = vc
            // if you already have your data object
            self.infos.otherUser = theDataDict
        }
    
        if let vc = segue.destination as? BusinessProfilePostsFeed,
            segue.identifier == "feedViewEmbedSegue" {
            self.feeds = vc
            // if you already have your data object
            self.feeds.otherUser = theDataDict
        }
    
        // etc
    
    }
    

    Now you'll have persistent references to the actual View Controllers embedded in your Container Views, in case you want access to them in other parts of your root VC, e.g.:

    @IBAction func btnTapped(_ sender: Any) {
        self.feeds.otherUser = theDataDict
    }
    
    0 讨论(0)
提交回复
热议问题