prepareForSegue **总是**创建一个新的destinationViewController? [英] prepareForSegue **always** creates a new destinationViewController?

查看:123
本文介绍了prepareForSegue **总是**创建一个新的destinationViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到以下代码总是会创建一个新的TagsFeedViewController。这是segues的默认行为吗?有没有办法配置iOS每次都不创建新的destinationViewController?

I Just realized that the following code always creates a new TagsFeedViewController. Is this the default behavior of segues? Is there a way to configure iOS to not create a new destinationViewController every time?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showSearchResult"]) {
        TagsFeedViewController *destViewController = segue.destinationViewController;
        destViewController.query = query;
    }
}


推荐答案

Segues使用提供给 - initWithIdentifier:source:destination:方法的视图控制器。创建目标视图控制器不是segue,而是故事板。来自文档

Segues use whichever view controllers are provided to their – initWithIdentifier:source:destination: methods. It's not the segue that creates the destination view controller, but the storyboard. From the docs:


通常,实例化故事板中的视图控制器并自动创建
以响应
故事板本身内定义的操作。

Normally, view controllers in a storyboard are instantiated and created automatically in response to actions defined within the storyboard itself.

所以你有一些选择:


  • 子类UIStoryboard。可能是一个坏主意。 UIStoryboard的公共接口只有三种方法; 故事板本身定义的行为并不公开,我认为没有足够的信息可以让你做正确的工作。

  • Subclass UIStoryboard. Probably a bad idea. The public interface for UIStoryboard has only three methods; the "actions defined within the storyboard itself" aren't public, and I don't think there's enough information available to let you do the job right.

让您的目标视图控制器成为单身。也是一个坏主意。除了单身人士带来的一般不良行为之外,你不需要保留一个没有视图的视图控制器。周围没有儿童视图控制器。并且使你的视图控制器类只是为了欺骗UIStoryboard使用你的视图控制器类的特定实例似乎有点蠢。

Make your destination view controller a singleton. Also a bad idea. Aside from the general badness that singletons bring with them, you shouldn't need to keep a view controller that has no views and no child view controllers around. And making your view controller class a singleton just to fool UIStoryboard into using a particular instance of your view controller class seems kinda icky.

子类UIStoryboardSegue。 如果您创建自己的segues,您可以在 - initWithIdentifier:source:destination:中执行您喜欢的操作,包括忽略提供的目标视图控制器并使用你想要的。这似乎仍然违背框架,这通常是一个糟糕的计划,但如果你绝对必须使用目标视图控制器的特定实例,这似乎是一个更好的方法。

Subclass UIStoryboardSegue. If you create your own segues, you can do what you like in – initWithIdentifier:source:destination:, including ignoring the provided destination view controller and using the one you want instead. This still seems like working against the framework, and that's usually a poor plan, but if you absolutely must use a particular instance of your destination view controller this seems like a better way to go.

顺其自然。最佳选择。想想你希望转向现有视图控制器的原因。考虑是否有更好的方法来实现您想要的而不必破坏框架。例如,您是否要使用现有的视图控制器,因为它已经具有某种特定的状态?也许最好在模型中维护该状态而不是在视图控制器中。

Go with the flow. Best option. Think about the reason that you're hoping to segue to an existing view controller. Consider whether there might be better ways to accomplish what you want without having to subvert the framework. For example, do you want to use an existing view controller because it already has some particular state? Maybe it'd be better to maintain that state in your model and not in the view controller.

这篇关于prepareForSegue **总是**创建一个新的destinationViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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