Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

like image 434
Lukas Bimba Avatar asked Feb 20 '18 17:02

Lukas Bimba


People also ask

How do I pass data from one ViewController to another view controller in Swift?

create bridge by Control + click on viewcontroller 2. perform segue for IBAction with id and pass data to the secondVC from firstVC. After creating segue using any one of the way, override the prepare(for segue: UIStoryboardSegue, sender: Any?) method to pass data.

How do I embed a view controller in navigation controller storyboard?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

What is the difference between View and View Controller?

A view controller is not drawable to the screen directly, it manages a group of view objects. View controllers usually have a single view with many subviews. The view controller manages the state of these views. A view controller is smart, and it knows how to interact with your application.


1 Answers

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
}
like image 147
DonMag Avatar answered Sep 22 '22 14:09

DonMag