如何在多个 UIViewController 之间共享一个故事板场景 [英] How to share one storyboard scene between multiple UIViewControllers

查看:16
本文介绍了如何在多个 UIViewController 之间共享一个故事板场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全重新制定了这个问题,因为我知道我最初偏离了轨道,但我离解决问题更近了.参考这张图片...

我想在代码中创建或操作 segue(以黄色突出显示),以便主视图是 MFMasterViewController(以红色突出显示)的多个子类中的任何一个.

当使用 Nibs 时,我可以创建一个 Nib,SharedNib.xib &将类设置为 MFMasterViewController,然后创建我的子类,比如 MFMasterViewControllerSubclassAMFMasterViewControllerSubclassB 等等.然后实例化我想要使用的任何子类...

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

或...

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

关于我如何使用故事板正确处理的任何线索?

就我而言,想要这样做的原因是我所有的子类都是相同的 tableview &数据但排序不同写入 cel 的详细文本的内容有所不同.我怀疑这是一种并不少见的模式.

干杯&TIA,佩德罗:)

解决方案

这不是一个直接的答案,但这是我根据你对原因的解释来完成你想要的.

基本上,您需要将 UITableViewDataSource(可能还有委托)与 MFMasterViewController 分开,以便在执行 segue 时,您可以在视图控制器.

因此在导航控制器中,您需要实现 prepareForSegue:sender: 方法.这是您可以在执行之前自定义 segue 的地方:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {//您可以使用 Interface Builder 设置 segue 标识符//确保您使用的是哪个 segue 也是一件好事if (([segue identifier] isEqualToString:@"TheId"]) {id数据源 = [[TableViewDataSourceImplementationA alloc] init];[[[segue destinationViewController] tableView] setDataSource:dataSource];}}

通过这种方式,您可以获得所需的自定义,而无需创建视图控制器的子类.

如果您可以访问 WWDC 视频,请查看会议 #407 在您的应用中采用故事板.

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

解决方案

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.

这篇关于如何在多个 UIViewController 之间共享一个故事板场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆