使用storyboard时重用UIViewController实例 [英] Reuse UIViewController instances when using storyboard

查看:58
本文介绍了使用storyboard时重用UIViewController实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在当前的iPhone应用程序中使用故事板。我面临一些问题。我真的需要重用我的UIViewController实例。

I decided to give the use of storyboards a go in my current iPhone app. I am facing a bit of a problem. I really need to reuse my UIViewController instances.

我的意思是什么?好吧,例如我有一个表视图控制器。当我点击一个单元格时,从故事板加载另一个视图控制器并将其推送到导航控制器堆栈。这一切都运行良好,但每次加载此视图控制器大约需要半秒到一秒。在我使用故事板之前,我只是通过缓存创建的实例来解决这个问题,这样第二次点击一个单元格就可以立即显示视图控制器。

What do I mean by that? Well, for example I have a table view controller. When I tap a cell, another view controller is loaded from the storyboard and pushed onto the navigation controller stack. This all works well, but it takes about half a second to a second each time this view controller is loaded. Before I was using story boards I simply solved this problem by caching the created instance so the second time you tap a cell the view controller can be immediately shown.

通过缓存创建实例我的意思是这样的:

By caching the created instance I mean something like this:

if (!cachedInstance) {
    cachedInstance = [MyViewController new];
}
[self.navigationController pushViewController:cachedInstance];

有谁知道如何使用故事板完成此操作?在此先感谢。

Does anyone know how to accomplish this using the storyboard? Thanks in advance.

推荐答案

如果您使用的是segues,则需要创建自定义segue才能使用缓存的视图控制器就像你之前做的那样。否则,典型的pushsegue将为 segue.destinationViewController 创建一个新的视图控制器实例。如果您编写自定义 UIStoryboardSegue 类并使用自定义 segue 你可以覆盖 initWithIdentifier:source:destination:并将你的缓存视图控制器放入 destinationViewController ,然后覆盖执行以使用经典 pushViewController 调用。

If you are using segues, you will need to create custom segues in order to use a cached view controller like you did before. Otherwise, the typical "push" segue will create a new instance of the view controller for segue.destinationViewController. If you write a custom UIStoryboardSegue class and use custom segues you can override initWithIdentifier:source:destination: and put your cached view controller in for the destinationViewController, and then override perform to use the classic pushViewController call.

那如果你真的想要使用它们,你是如何处理segue的。我会跳过它,除非你真的想要花哨的箭头在你的故事板上放置一切。如果你跳过它,你可以将视图控制器实例化到缓存中,然后像以前一样将它们推开。

That is how you handle the segue if you are really intent on using them. I would just skip it though, unless you really want the fancy arrows to lay everything out on your storyboard. If you skip it you can just instantiate the view controllers into the cache and then push them on just like you did before.

如果您的问题更多的是关于定位视图控制器在故事板中你可以使用:

If your question is more about locating a view controller inside a storyboard then you can use:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"Some View Controller"];

然后你可以将它保存到你的缓存中并像你在示例代码中那样推送它。

Then you can save that to your cache and push it like you did in your example code.

希望有所帮助。

这篇关于使用storyboard时重用UIViewController实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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