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

问题描述

我有一个iPad应用程序,它有很多屏幕和很多segue选项。
目前,我只是使用performSegueWithIdentifier来启动这些段,我担心的是,当用户执行越来越多的segues时,我占用了大量内存。
我见过人们推荐使用函数popToRootViewControllerAnimated:如果使用UINavigationController,但问题是我没有使用它。
如何阻止VC的数量激增?
应用程序的工作方式,用户不断返回根VC - 实际上是一个搜索屏幕。因此,如果我需要在需要这样一个segue时清除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.

推荐答案

当您使用segues时,流程会前后移动。当用户向后移动(即按下后退)时,它将不会推送到新的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.

如果在流程中有segues向后移动那么这是错误的。你只需要segue继续前进。

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

正确的SEGUE准备

在为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

如果你有随机流量(比如你的话)评论)然后你可以使用展开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然后只使用标准按下和弹出(从segue和后退按钮)。

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的展开segue。

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

如果你有一个取消按钮或控制器C中的东西,这是在Interface Builder中,这应该会带你回到控制器A.然后在控制器下面的Interface Builder中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。

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

08-11 19:36