Segues 和清除内存中的历史 ViewControllers [英] Segues and clearing historical ViewControllers from memory

查看:23
本文介绍了Segues 和清除内存中的历史 ViewControllers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iPad 应用,它有很多屏幕和很多 segue 选项.目前,我只是使用 performSegueWithIdentifier 来启动这些 segue,我担心随着用户执行越来越多的 segue,我会占用大量内存.我已经看到人们推荐使用函数 popToRootViewControllerAnimated: 如果使用 UINavigationController,但问题是我没有使用它.我怎样才能阻止 VC 的数量激增?应用程序的工作方式,用户确实不断返回到根 VC - 实际上是一个搜索屏幕.因此,如果我可以在需要这样的转场时清除 VC 的堆栈,那么我认为这将解决我的问题,但我不知道如何解决这个问题.感谢您的任何建议.

I have an iPad app which has a lot of screens and a lot of segue options. At the moment, I am simply using performSegueWithIdentifier to initiate these segues, and my fear is that I'm taking up a lot of memory as the user performs more and more segues. I've seen that people recommend using the function popToRootViewControllerAnimated: if using a UINavigationController, but the problem is that I'm not using one. How can I stop the number of VC's proliferating? The way the app works, the user does constantly return to the root VC - effectively a search screen. So if I could clear the stack of VC's when such a segue is needed, that would solve my issue I think, but I have no idea how to go about this. Thanks for any suggestions.

推荐答案

当您使用 segue 时,流程会前后移动.当用户向后移动(即按下返回")时,它不会推送到新的 VC,而是会弹出到已经存在的 VC.当你弹出时,当前的 VC 从堆栈和内存中移除.

When you are using segues the flow moves backwards and forwards. When the user moves backwards (ie presses "back") then it will not push to a new VC but it will pop to a VC that already existed. When you pop, the current VC is removed from the stack and memory.

如果您有转场在流程中向后移动,那么这是错误的.你只需要继续前进.

If you have segues to move backwards in the flow then this is wrong. You only need segues to move forward.

为 SEGUE 做好适当的准备

在准备转场时,您永远不应该创建自己的视图控制器并推送给它们.故事板可以为您完成所有这些工作.

In prepare for segue you should never create your own view controllers and push to them. The storyboard is there to do all of this for you.

一个合适的 prepareForSegue 方法应该是这样的...

A proper prepareForSegue method should look something like this...

- (void)prepareForSegue:(UIStoryBoardSegue*)segue
{
    if([segue.identifier isEqualToString:"SomeSegue"])
    {
        MyNewViewController *controller = segue.destinationViewController;

        controller.someProperty = "some value to pass in";
    }
}

这就是你所需要的.请注意,仅当您打算将一些信息传递给新的视图控制器时才需要它.如果你没有向前传递任何东西,那么你根本不需要这个方法.

That is all you need. Note that you only need this if you intend to pass some information to the new view controller. If you are not passing anything forward then you don't need this method at all.

当方法结束时,新的 VC 将被故事板文件推送到屏幕上.

When the method ends the new VC will get pushed onto the screen by the storyboard file.

展开分段

如果你有一个随机流(比如你的评论),那么你可以使用 unwind segues 来实现这一点.

If you have a random flow (like in your comment) then you can use unwind segues to achieve this.

在你的A"视图控制器中有一个类似的功能......

In you 'A' view controller have a function like...

- (IBAction)someUnwindAction:(UIStoryboardSegue*)sender
{
    //some action to run when unwinding.
}

它需要接收一个 UIStoryboardSegue 对象.如果设置为 IBAction,您还可以从 Interface Builder 访问它.

It needs to receive a UIStoryboardSegue object. If set up as an IBAction you can also access it from Interface Builder.

现在,当您想要 A > B > C > B > A 时,只需使用标准的 push 和 pop(来自 segue 和 back 按钮).

Now when you want to go A > B > C > B > A then just used the standard push and pop (from the segue and back button).

当你想要 A > B > C > A 时,你可以使用控制器 C 的 unwind segue.

When you want to go A > B > C > A then you can use the unwind segue from controller C.

如果您在控制器 C 中有一个取消按钮或其他东西,并且它在界面生成器中,这应该会将您带回控制器 A.然后在控制器 C 下的界面生成器中,您将有一个带门的绿色小方块和一个箭头指向它.只需将取消按钮的动作指向该符号并选择someUnwindAction".(注意,unwindAction 在 A 中,按钮在 C 中.)然后 XCode 使用它把你弹回到 A 并处理删除任何内存和东西.如果您愿意,您也可以将其他信息发送回 A.

If you have a cancel button or something in controller C and this is in the Interface Builder and this should take you back to controller A. Then in the Interface Builder underneath controller C you will have a little green square with a door and an arrow pointing out of it. Just point the action of the cancel button to this symbol and choose "someUnwindAction". (Note, unwindAction is in A, button is in C.) XCode then uses this to pop you all the way back to A and deals with removing any memory and stuff. If you want you can send additional information back to A too.

如果您想以编程方式从 C 访问这个 unwind segue,那么您可以运行...

If you want to access this unwind segue from C programmatically then you can run...

[self performSegueWithIdentifier:"someUnwindAction" sender:nil];

这也会弹回 A.

这篇关于Segues 和清除内存中的历史 ViewControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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