Ios 多故事板:我应该使用单例模式来缓存它们吗?

Ios 多故事板:我应该使用单例模式来缓存它们吗?,ios,objective-c,cocoa-touch,storyboard,Ios,Objective C,Cocoa Touch,Storyboard,我在我的应用程序中使用了多个故事板,这被认为是一种良好的做法。例如,从Main.storyboard中的我的FooViewController,当点击一个按钮时,我将以编程方式跳转到Secondary.storyboard中定义的另一个BarViewController 故事板和视图控制器之间的转换在单独的文件中实现,例如,故事板,如下所示: // in Storyboards.m @implementation Storyboards + (id)instantiateBarViewCont

我在我的应用程序中使用了多个故事板,这被认为是一种良好的做法。例如,从
Main.storyboard
中的我的
FooViewController
,当点击一个按钮时,我将以编程方式跳转到
Secondary.storyboard
中定义的另一个
BarViewController

故事板和视图控制器之间的转换在单独的文件中实现,例如,
故事板
,如下所示:

// in Storyboards.m
@implementation Storyboards

+ (id)instantiateBarViewController {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    return [storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
}

@end
// in Storyboards.m
@implementation Storyboards

+ (id)instantiateBarViewController {
    static UIViewController *barViewController = nil;  // Shared view controller.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIStoryboard *storyboard = [self secondaryStoryboard];
        barViewController = [storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
    });
    return barViewController;
}

+ (UIStoryboard *)secondaryStoryboard {
    static UIStoryboard *storyboard = nil;  // Shared storyboard.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        storyboard = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    });
    return storyboard;
}

@end
然后在
FooViewController
中,我执行了以下操作:

// in FooViewController.m
- (IBAction)someButtonTapped:(id)sender {
    BarViewController *controller = [Storyboards instantiateBarViewController];
    [self.navigationController pushViewController:controller animated:YES];
}
这个很好用。但我的问题是:

  • 是否有必要缓存情节提要实例,以便每次调用
    InstanceBarViewController
    时,我都不需要重新创建情节提要
  • 如果是,我是否也应该缓存
    BarViewController
  • 要缓存情节提要(和视图控制器),我可以使用以下代码:

    // in Storyboards.m
    @implementation Storyboards
    
    + (id)instantiateBarViewController {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        return [storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
    }
    
    @end
    
    // in Storyboards.m
    @implementation Storyboards
    
    + (id)instantiateBarViewController {
        static UIViewController *barViewController = nil;  // Shared view controller.
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            UIStoryboard *storyboard = [self secondaryStoryboard];
            barViewController = [storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
        });
        return barViewController;
    }
    
    + (UIStoryboard *)secondaryStoryboard {
        static UIStoryboard *storyboard = nil;  // Shared storyboard.
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            storyboard = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        });
        return storyboard;
    }
    
    @end
    

    谢谢

    单例似乎有些过分-从情节提要实例化视图控制器只需:

    [[UIStoryboard Storyboard with name:@”“bundle:nil]实例化控件的标识符:@”“

    [[UIStoryboard Storyboard with name:@”“bundle:nil]实例化初始视图控制器]

    [self.storyboard实例化eviewcontrollerwhiteIdentifier:@”“

    保持简单我会说“不要。”

    像这样保留对资源的引用意味着你要在内存受限的设备上把东西塞进内存,而且永远不要让它们离开糟糕的开发人员!没有饼干;-)

    您有什么证据表明,这比让系统管理缓存或破坏从XIB重复加载的资源(确实如此)更好?我认为你的方法可能弊大于利。记住,简单地创建情节提要实例并不能直接转化为“每次都从文件系统加载它”。系统相当智能地管理它,因此不需要通过维护从未发布的引用来短路它


    如果您试图解决一些性能问题,我强烈建议您使用仪器来测量性能并从中着手。我严重怀疑这是故事板/xib加载问题。

    我认为这是一个过早优化的案例。我会采取不同的方法和使用方法:它允许您直观地链接故事板。非常强大tool@AshleyMills我对此也有疑问,这就是为什么在我的应用程序中实现它之前,我会问这个问题的原因@西蒙克洛林谢谢你的链接。我也知道这个项目,但我更喜欢用手工的方式来做。因为:1)我认为代码更容易理解;2) 正如该项目所说:“RBStoryboardLink一开始只是一个概念证明,现在的情况仍然是如此。”——但是如果你认为我的手动方法与RBStoryboardLink相比有一些严重的缺陷,或者明显的缺点,我很高兴知道并切换到RBStoryboardLink。谢谢你的回答!我投了更高的票,但我会接受第一个答案,这与你的意见几乎相同。:-)被接受的回答者又投了一票。我贴了我的,因为我觉得有必要详细说明。我的话太多了