On my storyboard, my project begins with a split view that automatically assigns my custom UITableViewController
(embedded in a navigation controller) as the detail view controller (done by relationship segue). How do I access the split view controls from my custom UITableViewController
so I can change the master view controller views as appropriate?
UIViewController has a property splitViewController
that is a reference to the split view controller the viewController is embedded inside. Since your table view controller is embedded inside a navigation controller, which is itself embedded inside a split view controller, you first need to get a reference to the nav controller, and then from that get its reference to the split view.
So in your custom tableViewController's code you can do this:
UISplitViewController *splitVC = [[self navigationController] splitViewController];
The from that you can get a reference to your masterViewController. The splitViewController has a property viewControllers
which is an NSArray of two elements. The element at index zero is the master viewController. The element at index 1 is your detail view controller.
UIViewController *masterVC = [[splitVC viewControllers] objectAtIndex:0];
Note that if your master is a custom viewController subclass (which it probably is) you should cast it as such when you pull it out of the array.
If you want to relace the master view controller with a new viewController entirely, you can do that by creating a new array with your new master VC and the existing detail view controller and assigning it to your split view controller's viewControllers
property:
UIViewController *detailVC = [[splitVC viewControllers] objectAtIndex:1];
NSArray *newViewControllerArray = [NSArray arrayWithObjects:newMasterVC, detailVC, nil];
splitVC.viewControllers = newViewControllerArray;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With