[英]Using a custom segue for moving backwards on a navigation stack
I'm moving my App to Storyboards and, so far, so good. 我正在将我的应用程序移动到情节提要,到目前为止,一切都很好。
However, I've found something that I don't really understand and worries me. 但是,我发现了一些我不太了解的东西,这让我感到担心。 I would appreciate if someone can provide some insight on this.
如果有人可以对此提供一些见解,我将不胜感激。
My app uses a normal Navigation Controller. 我的应用程序使用普通的导航控制器。 For moving "forward" to new View Controllers, I'm using custom segues;
为了将“转发”移至新的View Controller,我使用了自定义参数。 no problems there.
那里没有问题。 However, there's a point in the App where I want to move back to the beginning of the Navigation Stack.
但是,在应用程序中有一点我想移回导航堆栈的开头。 I have also configured that "navigation" using a custom segue, for that, I created the segue in Interface Builder by dragging the last view controller to the first one (that already looks weird to me), and I've implemented the custom segue perform method in the following way:
我还使用定制的segue配置了“导航”,为此,我在Interface Builder中通过将最后一个视图控制器拖到第一个视图控制器(对我来说已经很奇怪)创建了segue,并且实现了定制的segue。按以下方式执行方法:
-(void)perform
{
UIViewController *src = (UIViewController *)self.sourceViewController;
UIViewController *dest = (UIViewController *)self.destinationViewController;
[src.navigationController popToRootViewControllerAnimated:NO];
// Custom animation code here
}
... It works great. ...效果很好。 However, I don't understand why it works.
但是,我不明白为什么会这样。 In my mind, the custom segue should be instantiating a new instance of my first view controller and assign it as "dest", but it looks like the segue is smart enough to realize I want to navigate to a previous, existent, instance of a View Controller and, instead of creating a new instance, it assigns to "dest" the existing one.
在我看来,自定义segue应该实例化我的第一个视图控制器的新实例并将其分配为“ dest”,但是看起来segue足够聪明,以至于我想导航到以前存在的a实例View Controller,而不是创建新实例,而是将“实例”分配给现有实例。
Does anybody know if using segues in this way is ok? 有人知道以此方式使用segue是否可以吗? Is it possible that it works by chance but might stop working in the future?
它是否有可能偶然发生但将来可能会停止工作? Am I wasting memory in anyway as the segue is instantiating a View Controller I'm not going to use?
无论如何,我是在浪费内存,因为segue实例化了我将不使用的View Controller?
Thanks a lot in advance! 在此先多谢!
Am I wasting memory in anyway as the segue is instantiating a View Controller I'm not going to use?
无论如何,我是在浪费内存,因为segue实例化了我将不使用的View Controller?
DestinationController
property for your custom segue.
DestinationController
属性。
Test by yourself : add a static counter into your root controller, increment it each time this class is initialized and display it in your view : you'll see it getting incremented every time you pop to root using this trick.
Does anybody know if using segues in this way is ok?
有人知道以此方式使用segue是否可以吗?
There's at least one solution to this problem : release the DestinationController
of the segue in your (void)perform
implentation. 有至少一个解决这个问题:释放
DestinationController
的SEGUE在你的(void)perform
implentation。 This is really quick to implement, but kinda ugly since you allocate and immediately release your view controller every time... even if it's better than just leaking it, it's not what I'd call a good practice! 这确实很容易实现,但是有点麻烦,因为每次分配并立即释放视图控制器时……即使比泄漏它更好,但这也不是一个好习惯! To my mind, a better way to achieve what you want would be to not use a segue for that transition, just to use a button or whatever and call
popToRootViewController:animated
when getting a touch on this button. 在我看来,实现所需目标的更好方法是不使用segue进行过渡,而只需使用按钮或其他按钮,并在触摸此按钮时调用
popToRootViewController:animated
。
Is it possible that it works by chance but might stop working in the future?
它是否有可能偶然发生但将来可能会停止工作?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.