Ios 如何在多个UIViewController之间共享一个情节提要场景

Ios 如何在多个UIViewController之间共享一个情节提要场景,ios,uiviewcontroller,segue,uistoryboard,uistoryboardsegue,Ios,Uiviewcontroller,Segue,Uistoryboard,Uistoryboardsegue,我完全重新制定了这个问题,因为我知道我最初偏离了轨道,但我离解决问题还不远。关于这张图片。。。 我想在代码中创建或操作segue(以黄色突出显示),以便主视图是MFMasterViewController(以红色突出显示)的多个子类中的任意一个 使用Nib执行此操作时,我可以创建一个Nib,SharedNib.xib&将类设置为MFMasterViewController,然后创建我的子类,例如MFMasterViewControllerSubclassA,MFMasterViewContro

我完全重新制定了这个问题,因为我知道我最初偏离了轨道,但我离解决问题还不远。关于这张图片。。。

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

使用Nib执行此操作时,我可以创建一个Nib,
SharedNib.xib
&将类设置为
MFMasterViewController
,然后创建我的子类,例如
MFMasterViewControllerSubclassA
MFMasterViewControllerSubclassB
等,然后实例化我想要使用的子类

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

等等

关于我如何使用故事板正确处理这个问题,有什么线索吗

在我的例子中,希望这样做的原因是我的所有子类都是相同的tableview和数据,但排序不同&在CEL的详细文本中写入的内容有所不同。我怀疑这是一种常见的模式

干杯,蒂亚,
佩德罗:)

如果你的.m文件与任何情节提要都没有关联,self.storyboard不是零吗

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                   @"MainStoryboard" bundle:[NSBundle mainBundle]];
    ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
确保将名为的故事板更改为故事板的名称

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"];

这不是一个直接的答案,但这是我如何根据你对原因的解释实现你想要的

基本上,您需要将
UITableViewDataSource
(可能还有委托)与
MFMasterViewController
分开,以便在执行segue时,可以在视图控制器中设置正确的数据源和委托

因此,在导航控制器中,您需要实现
prepareforsgue:sender:
方法。在这里,您可以在执行segue之前对其进行自定义:

- (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];
    }
}
-(void)prepareforsgue:(UIStoryboardSegue*)分段发送方:(id)发送方{
//您可以使用Interface Builder设置序列标识符
//另外,确保使用的是哪个序列也是一件好事
if(([segue identifier]IsequalString:@“TheId”]){
id dataSource=[[TableViewDataSourceImplementationA alloc]init];
[[[segue destinationViewController]tableView]设置数据源:数据源];
}
}
这样,您就可以获得所需的自定义,而无需创建视图控制器的子类


如果你有机会访问WWDC视频,检查你的应用程序中使用故事板的会话407。

对于任何绊倒这个问题的人,你也应该考虑更一般地使用一个“策略”。模式作为控制器子类化的替代方法。可接受的答案就是这样的一种形式,其中实现的策略来自于任何数据源/委托,并且可以在运行时切换。另一个例子是

我相信我终于找到了答案。我们希望将storyboard ViewController与另一个类一起使用e、 有很多解决方法,比如使用代理,但我认为这是最好的。我已经在另一个主题中回答了这个问题。希望它能有所帮助!

我知道我可以通过在故事板中为每个子类制作一个场景副本来实现这一点,但当场景完全相同时,这似乎是一种非常浪费的方式&所有版本都会切换到同一个下一个场景。故事板中的控制器是否分配给类“MyViewController”以及
MyViewControllerSubclass
sub
MyViewController
类对这两个问题都是:)我不明白您为什么试图将
controller
分配给另一个名为
newViewController
UIViewController
?我将一起去掉
UIViewController
,并将第一行更改为:
MyViewControllerSubclass*controller=(MyViewControllerSubclass*)[[self-storyboard]instanceeviewcontrollerwhiteIdentifier:@“SharedScene”]
另外,确保您的子类在
init
函数中调用
[super init]
。这是一种简写。实际上,我在使用XIBs时使用的是
UIViewController*newViewController=[self-methodthattinstancessubclass]其中,根据各种条件,
MyViewControllerSubclassA
MyViewControllerSubclassB
等由方法分配。在我的示例中,我调用的类MyViewController确实属于我故事板中的场景&我问题工作中的前两个代码示例。MyViewControllerSubclass是MyViewController的一个子类,但调用它的代码失败。-(id)initWithIdentifier:(NSString)identifier source:(UIViewController*)source destination:(UIViewController*)destination{UIStoryboard*storyBoard=[UIStoryboard storyBoard WithName:@“Mainstryboard_iPhone”bundle:nil];UIViewController*viewController=[storyBoard InstanceViewController WithiIdentifier:@“testIdentifier”];//MyViewController viewController=[[MyViewController alloc]initWithNibName:@“MyViewController”捆绑包:nil];返回[super-initWithIdentifier:标识符源目标:viewController];]
- (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];
    }
}