Passing Data to view controllers that are embedded in container views

我是研究僧i 提交于 2019-12-30 04:59:10

问题


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 user using container views. I know all of these views will be loaded at the same time and will stay loaded, which is what I want even though the toll on memory. I know how to directly pass this value to the view controller but don't know how to pass it to a view controller that would then spread it to 4 views to load the same data. ---- Below is me passing "otherUser" to "BusinessProfileSwitchView"(View Controller with container views) based on the search bar actions.

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "BusinessProfiles" {
        // gotta check if we're currently searching
        if self.searchController.isActive && searchController.searchBar.text != "" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let user = filteredUsers[indexPath.row]
                let controller = segue.destination as? BusinessProfileSwitchView
                controller?.otherUser = user
            }
        } else {
            if let indexPath = tableView.indexPathForSelectedRow {
                let user = usersArray[indexPath.row]
                let controller = segue.destination as? BusinessProfileSwitchView
                controller?.otherUser = user
            }
        }
    }
}

What is the method of attack do you guys think I should use to pass "otherUser"/NSDictionary to the view controller with container views that would then spread "otherUser" to 4 views? Below is my view controller that connect to the other 4 views.

 import UIKit

 class BusinessProfileSwitchView: UIViewController {

@IBOutlet weak var feedView: UIView!
@IBOutlet weak var collectionView: UIView!
@IBOutlet weak var infoView: UIView!
@IBOutlet weak var socialView: UIView!

var infos: BusinessProfilesDetails!
var collections: BusinessProfilePostsCollection!
var feeds: BusinessProfilePostsFeed!
var socials: BusinessProfilesViewController!

@IBOutlet weak var switchController: UISegmentedControl!

var otherUser: NSDictionary!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    switch  switchController.selectedSegmentIndex {
    case 0:
        infoView.isHidden = false
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 1:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = false
        socialView.isHidden = true
        break
    case 2:
        infoView.isHidden = true
        feedView.isHidden = false
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 3:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = false
        break
    default:

        break;
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func viewControl(_ sender: UISegmentedControl) {
    switch  switchController.selectedSegmentIndex {
    case 0:
        infoView.isHidden = false
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 1:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = false
        socialView.isHidden = true
        break
    case 2:
        infoView.isHidden = true
        feedView.isHidden = false
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 3:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = false
        break
    default:

        break;
    }
}

}


回答1:


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
}


来源:https://stackoverflow.com/questions/48890543/passing-data-to-view-controllers-that-are-embedded-in-container-views

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