Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

ios - Passing Data to view controllers that are embedded in container views

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...