ViewWillDisappear与dealloc [英] ViewWillDisappear versus dealloc

查看:104
本文介绍了ViewWillDisappear与dealloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的视图控制器的dealloc方法中放了一个NSlog。它不会被一致地调用。我注意到ViewWillDisappear总是被调用。可以在这里移动我所有整洁的upcode吗?将内容设置为Nil并释放调用。

I put an NSlog in my dealloc method of my view controller. It does not get consistently called. I do notice that ViewWillDisappear does get called always. Would it be ok to move all my tidy upcode here? Setting stuff to Nil and release calls.

有人为什么dealloc没有被调用得到一些建议?我知道它在文档中说它可能不会被调用,但如果你有一个非常简单的应用程序,它总是被调用。所以我做的事情必须影响dealloc。

Anybody got some advice on why dealloc is not getting called? I know it says in docs it may not get called, but if you have a very simple App it gets called always. So something i do must be affecting the dealloc.

这是调用我的ViewController而不是总是调用我的dealloc的代码。

This is the code that calls my ViewController than isnt always calling my dealloc.

-(IBAction) playComputerTapped:(id)sender
{

PlayGameViewController * pgvc = [[PlayGameViewController alloc]
initWithNibName:@PlayGameViewControllerbundle:[NSBundle mainBundle]];
pgvc.gameMode = 1;
[self presentModalViewController:pgvc animated:YES];
[pgvc release];
}

PlayGameViewController *pgvc = [[PlayGameViewController alloc] initWithNibName:@"PlayGameViewController" bundle:[NSBundle mainBundle]]; pgvc.gameMode = 1; [self presentModalViewController:pgvc animated:YES]; [pgvc release]; }

以上代码将我从mailmenu ViewController带入游戏。

The above code takes me from the mailmenu ViewController into the game.

下面是离开gameViewController并带我回到菜单的代码。

Below is the code to leave the gameViewController and take me back to the menu.

[self.parentViewController dismissModalViewControllerAnimated:YES];

谢谢
-Code

Thanks -Code

推荐答案

你的意思是 viewDidUnload 而不是 viewWillDisappear

viewWillDisappear 在视图控制器消失时被调用。这通常在弹出视图控制器或将其他视图控制器推送到堆栈时发生。 viewWillDisappear 的目的是停止活动 - 例如停止动画,隐藏某些元素或类似内容。

viewWillDisappear is called when the view controller is disappearing. This usually happens when the view controller is being popped out, or other view controller is pushed to the stack. Purpose of viewWillDisappear is to stop active actions - for example stop animations, hide some elements or similar.

viewDidUnload 可能是你的意思,因为在卸载视图控制器的视图时会调用这个。对于当前可见的视图控制器,这应该永远不会发生,仅适用于导航堆栈中某些位置的控制器(UITabBarController或UINavigationController的一部分),但当前不可见。 viewDidUnload 的目的是释放属于视图的任何UI元素,并且视图控制器也保留。

viewDidUnload is probably what you meant as this one is called when view controller's view is unloaded. This should never happen for a view controller that is currently visible, only for controllers that are somewhere in the navigation stack (part of UITabBarController or UINavigationController), but not currently visible. Purpose of viewDidUnload is to release any UI elements that are part of the view and that the view controller retained as well.

要理解为什么要卸载这种控制器的视图的原因很重要。原因是内存消耗。视图占用大量内存,即使它们当前不可见。视图通常很容易重建 - 只需调用构造它们的代码即可。如果您使用Interface Builder,这尤其容易。所以这些视图是获得更多内存的最佳候选者。

To understand it's important to realize the reasons why a view for such controller would want to be unloaded. Reason is memory consumption. Views consume considerable amount of memory, even when they are not currently visible. Views are usually very easy to reconstruct - simply by calling the code that constructed them in the first place. That's especially very easy if you are using Interface Builder. So these views are best candidates to be freed to gain more memory.

当系统没有足够的内存时,它开始调用 didReceiveMemoryWarning 当前不可见的视图控制器的方法。如果您已经在Xcode中使用模板创建了控制器,则此方法只需调用super(UIViewControllers)的实现 [super didReceiveMemoryWarning] 。该默认实现将释放 self.view ,这反过来应该将其与所有子视图一起解除分配。

When system doesn't have enough memory it starts calling didReceiveMemoryWarning method of view controllers which are not currently visible. If you have created your controllers from template in Xcode, this method simply calls super's (UIViewControllers's) implementation [super didReceiveMemoryWarning]. That default implementation will release self.view, which in turn should deallocate it together with all its subviews.

现在假设您的视图控制器需要访问某些子视图以某种方式操作它。例如,您可以在那里拥有UILabel元素,并且您希望相应地更改其内容。要访问此元素,您需要创建一个成员变量(IBOutlet)并将其连接到该元素。现在,您的视图控制器拥有该标签,因此其保留计数会增加。释放控制器视图时,标签也是如此,但由于视图控制器仍保留标签,因此不会取消分配。因此,您应该在 viewDidUnload 方法中发布标签。

Now let's say that your view-controller needs access to some of the subviews to manipulate it in some way. For example you can have a UILabel element there and you want to change its content accordingly. To access this element you create a member variable (IBOutlet) and connect it to the element. Now your view-controller owns that label, so its retain-count is increased. When controller's view is released, so is the label, but because your view-controller still retains the label, it will not be deallocated. Therefore you should release the label in viewDidUnload method.

我见过以编程方式创建视图的应用程序(在 loadView 方法),但加载是以如此脏的方式完成的,以至于在一次取消分配后无法重建视图。因此,每次系统内存不足时,它都会调用 didReceiveMemoryWarning ,然后取消分配视图,但在导航回该视图控制器应用程序后崩溃了。一个快速的错误修正是在视图控制器中删除调用 [super didReceiveMemoryWarning] 。好吧,系统没有得到内存并且发生了一些奇怪的效果,但至少应用程序没有立即崩溃。

I've seen applications that were creating views programmatically (in loadView method), but loading was done in such dirty way that it was not possible to reconstruct the view after it was once deallocated. Therefore each time system was out of memory, it had called didReceiveMemoryWarning which in turn deallocated the view, but after navigating back to that view-controller application had crashed. A fast "bugfix" was to remove calling [super didReceiveMemoryWarning] in view-controllers. Well, system didn't get the memory and some strange effects occurred, but at least the application didn't crash immediately.

现在第三个 - 的dealloc 。当对象不属于任何人并且其内存将被释放时,将调用此方法。在这里,您需要释放您保留的所有对象。对于视图控制器,这些通常是对模型类的引用。

Now the third one - dealloc. This is called when object is not owned by anyone and its memory is going to be freed. Here you need to release all objects that you have retained. For view-controllers those are usually references to model classes.

我想描述一个可能的场景。假设您有一个视图控制器显示与其他人的聊天。让我们说这是非常花哨的聊天,表情符号和好友图标都是动画的。假设每个聊天项都显示为UITableView的单元格。

I want to describe one more possible scenario. Let's say you have a view-controller displaying a chat with another person. Let's say it's very fancy chat, with emoticons and buddy-icons being animated. Let's say that each chat-entry is displayed as a cell of UITableView.

当您的好友向您发送消息时,您希望将新单元格附加到表格视图中重装它。因此你的视图控制器有一个表视图的出口。

When your buddy sends you a message, you want to append a new cell into table-view by reloading it. Therefore your view-controller has an outlet to the table-view.

viewWillDisappear 你应该停止动画表情符号和图标。
viewDidUnload 中,您应该释放表视图。
dealloc 中,您想要发布聊天记录(可能是此会话期间发送和接收的所有消息的NSArray)。

In viewWillDisappear you should stop the animations of emoticons and icons. In viewDidUnload you should release the table-view. In dealloc you want to release chat's history (probably NSArray of all messages sent and received during this conversation).

现在,如果你离开聊天, viewWillDisappear 会被调用,你就会停止动画。

Now if you navigate away from your chat, viewWillDisappear gets called and you stop the animations.

当系统内存不足,并且您的视图控制器不可见时,将调用 didReceiveMemoryWarning 并释放视图。调用你的 viewDidUnload 并释放UITableView,以便它可以真正解除分配。

When system is low on memory, and your view-controller is not visible, didReceiveMemoryWarning gets called and the view is released. Your viewDidUnload gets called and you release UITableView so that it can be really deallocated.

当你导航回到聊天,再次调用 loadView 并再次构建视图,之后调用 viewDidLoad 。您的模型(聊天对话的表示)仍然存在,因此表视图的数据源具有与以前一样的所有数据,因此表视图将显示与取消分配视图之前完全相同的内容。毕竟 viewWill / DidAppear 在你启动动画的地方被调用。

When you navigate back to the chat, loadView gets called again and your view is constructed again, viewDidLoad is called after that. Your model (representation of chat conversation) is still there, so the table-view's datasource has all the data as before, so table-view will display exactly the same thing as before the view was deallocated. After all viewWill/DidAppear is called where you start the animations.

当你和朋友聊天时,你释放视图控制器,并取消分配 - 调用 dealloc ,释放包含聊天消息的数组,并清除其他所有内容。

When you finish chatting with your friend, you release the view controller, and it gets deallocated - dealloc is called, you release an array containing the chat messages, and cleanup everything else.

我希望它能让事情变得更加清晰。

I hope it make things a little clearer.

这篇关于ViewWillDisappear与dealloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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