How to share one storyboard scene between multiple UIViewControllers

女生的网名这么多〃 提交于 2019-11-30 16:47:03

问题


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...

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 :)


回答1:


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.




回答2:


For anyone stumbling upon this question, you should also consider more generally using a "Strategy" pattern as an alternative to subclassing your controller. The accepted answer is a form of that, where the strategy implemented comes from whatever the data source/delegate is, and can be switched out at runtime. Another example of this is https://stackoverflow.com/a/17381927/954643




回答3:


If your .m file is not associated with any storyboard, wouldn't self.storyboard be Nil?

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                   @"MainStoryboard" bundle:[NSBundle mainBundle]];
    ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];

Make sure to change the storyboardWithName: to whatever your storyboard is named.

NSString * storyBoardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyBoardName = @"MainStoryboard_iPad";
} else {
    storyBoardName = @"MainStoryboard_iPhone";
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                   storyBoardName bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];



回答4:


I believe i finally found the answer. We want to use storyboard ViewController with another class name. There are many workarounds like using delegates but i think this is the best one. I already answered it in another topic. Hope it helps! https://stackoverflow.com/a/32103618/1943053



来源:https://stackoverflow.com/questions/12268888/how-to-share-one-storyboard-scene-between-multiple-uiviewcontrollers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!