Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share one storyboard scene between multiple UIViewControllers

I'm completely re-formulating this question having learned that I was originally off track but that having me no closer to solving the problem. With reference to this image... storyboard illustration

I am wanting to either create or manipulate the segue (highlighted in yellow) in code such that the Master view is any one of a number of subclasses of MFMasterViewController (highlighted in red).

When doing this using Nibs I could create a Nib, SharedNib.xib & set the class as MFMasterViewController, then create my subclasses, say MFMasterViewControllerSubclassA, MFMasterViewControllerSubclassB etc. & then instantiate whichever subclass I wanted using...

MFMasterViewControllerSubclassA *controller = [[MFMasterViewControllerSubclassA alloc] initWithNibName:@"SharedNib" bundle:nil];

or...

MFMasterViewControllerSubclassB *controller = [[MFMasterViewControllerSubclassB alloc] initWithNibName:@"SharedNib" bundle:nil];

etc.

Any clues as to how I can get this right using storyboards?

In my case the reason for wanting to do this is that all my subclasses are the same tableview & data but sorted differently & having some difference in what's written to the detail text of the cels. I suspect that it is a not uncommon pattern.

Cheers & TIA, Pedro :)

like image 503
Pedro Avatar asked Sep 04 '12 17:09

Pedro


People also ask

Can we use multiple storyboards in one application?

Apple introduced them in iOS 9 and macOS 10.11. They do exactly what I needed. They allow you to break a storyboard up into multiple, smaller storyboards. A storyboard reference ties multiple storyboards together, creating one, large, composite storyboard.


1 Answers

It's not a direct answer but this is how I would accomplish what you want based on your explanation of the reason.

Basically you need to separate the UITableViewDataSource (and maybe the delegate too) from the MFMasterViewController so when the segue is executed you can set the correct dataSource and delegate in the view controller.

So in the Navigation Controller you need to implement the prepareForSegue:sender: method. This is where you can customize the segue before it is executed:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // you can set the segue identifier using Interface Builder 
    // also it is a good thing to make sure which segue you're using
    if (([segue identifier] isEqualToString:@"TheId"]) {
       id<UITableViewDataSource> dataSource = [[TableViewDataSourceImplementationA alloc] init];
       [[[segue destinationViewController] tableView] setDataSource:dataSource];
    }
}

This way you can get the customization you want without the need to create subclasses of your view controller.

And if you have access to WWDC videos, check the session #407 Adopting Storyboards in Your App.

like image 63
Felipe Cypriano Avatar answered Sep 21 '22 03:09

Felipe Cypriano