user8637139
user8637139

Reputation: 43

iOS Swift 4 - how to pass data to splitviewcontrollers master

If anyone is well experienced with split view controllers and can help me with, it would be a great help. I have a project that begins with a login screen, followed by a search screen that calls an API to get the results. After the search screen gets the results from the API call, I want to pass those to a splitviewcontroller for display. Basically to the splitviewcontroller’s master, which has the table view. So how to do this? I don’t see how can I use performsegue, when the segue’s from splitviewcontroller to the master don’t have any identifiers? So is it possible to use splitviewcontroller as your third or fourth screen? Is there any alternate way to achieve this?

Thanks.

Upvotes: 1

Views: 1775

Answers (1)

Brandon A
Brandon A

Reputation: 8289

Every UIViewController has a property called splitViewController. This is an optional value that may or may not be nil based on if you are using a UISplitViewController. The splitViewController property will allow you to access the view controllers that the split view controller has embedded.

With UISplitViewControllers there are two main components, the master and detail view controllers. On iPad the master is the one that is on the left side of the screen and takes up the least amount of space and the detail will take up the large area to the right. On iPhone both will be on top of each other in a navigation stack and both will take up the entire screen.

This is how to access the detail from the master:

if let splitVC = self.splitViewController, let detailVC = splitVC.viewControllers[1] {
    detailVC.doSomething()
}

And this is how to access the master from the detail:

if let splitVC = self.splitViewController, let masterVC = splitVC.viewControllers[0] {
    masterVC.doSomething()
}

Keep in mind you may be using a UINavigationController or UITabBarController within your storyboard that the master or detail may be embedded within. In that case you do the same but cast those view controllers as either UINavigationController or UITabBarController and drill down within them to retrieve your true master and detail.

Upvotes: 5

Related Questions