UINavigationController崩溃,因为推和UIViewControllers [英] UINavigationController crash because of pushing and poping UIViewControllers
问题描述
我的问题与我发现UINavigationController崩溃的原因有关。所以我会先告诉你关于发现。请裸露我。
My question is related to my discovery of a reason for UINavigationController to crash. So I will tell you about the discovery first. Please bare with me.
问题:
我有一个UINavigationController作为UIWindow的子视图,一个rootViewController类和一个自定义MyViewController类。以下步骤将获得Exc_Bad_Access,100%可再现。:
The issue: I have a UINavigationController as as subview of UIWindow, a rootViewController class and a custom MyViewController class. The following steps will get a Exc_Bad_Access, 100% reproducible.:
[myNaviationController pushViewController:myViewController_1stInstance animated:YES];
[myNaviationController pushViewController:myViewController_2ndInstance animated:YES];
点击左侧的tapBarItem两次(弹出两个myViewController实例)以显示rootViewController。
Hit the left back tapBarItem twice (pop out two of the myViewController instances) to show the rootViewController.
经过痛苦的半天的尝试和错误后,我终于找出了答案,但也提出了一个问题。
After a painful 1/2 day of try and error, I finally figure out the answer but also raise a question.
The Solutio:我在.m文件中声明了许多对象,作为一种惰性的方式来声明私有变量,以避免混淆.h文件。例如,
The Solutio: I declared many objects in the .m file as a lazy way of declaring private variables to avoid cluttering the .h file. For instance,
#impoart "MyViewController.h"
NSMutableString*variable1;
@implement ...
-(id)init
{
...
varialbe1=[[NSMutableString alloc] init];
...
}
-(void)dealloc
{
[variable1 release];
}
由于某些原因,iphone操作系统可能会丢失这些lazy private变量内存分配当myViewController_1stInstance的视图卸载(但仍然在导航控制器的堆栈)在加载myViewController_2ndInstance的视图后。第一次点击后面tapBarItem是确定,因为myViewController_2ndInstance'view仍然加载。但是第二个tap在后面tapBarItem给了我地狱,因为它试图解除第一个实例。它调用[variable release]导致Exc_Bad_Access,因为它随机指向(松指针)。
For some reasons, the iphone OS may loose track of these "lazy private" variables memory allocation when myViewController_1stInstance's view is unloaded (but still in the navigation controller's stacks) after loading the view of myViewController_2ndInstance. The first time to tap the back tapBarItem is ok since myViewController_2ndInstance'view is still loaded. But the 2nd tap on the back tapBarItem gave me hell because it tried to dealloc the 1st instance. It called [variable release] resulted in Exc_Bad_Access because it pointed randomly (loose pointer).
要解决这个问题很简单,在.h文件中将variable1声明为@private。
To fix this problem is simple, declare variable1 as a @private in the .h file.
这是我的问题:
我一直使用lazy private变量一段时间没有任何问题,直到他们涉及UINavigationController。这是iPhone操作系统中的错误吗?或者我对我的目标C有一个根本的误解?请帮忙。
Here is my Question: I have been using the "lazy private" variables for quite some time without any issues until they are involved in UINavigationController. Is this a bug in iPhone OS? Or there is a fundamental misunderstanding on my part about Objective C? Please help.
推荐答案
它可能与您的视图控制器的两个实例使用相同的静态分配的变量相关。
It might be related to both instances of your view controller using the same statically-allocated variable.
换句话说, myViewController_1stInstance
和 myViewController_2ndInstance
在内存中使用相同的 variable1
位置并相互覆盖。
In other words, both myViewController_1stInstance
and myViewController_2ndInstance
are using the same variable1
location in memory and overwriting one another.
在 @interface
定义后,在大括号内声明的变量具有由运行时为每个在每次调用 [< ClassName> alloc]
时,在全局范围中声明的变量(即在任何函数或类声明之外)
Variables declared inside of the curly braces after your @interface
definition have a memory location allocated by the runtime for each instance of the class (every time you call [<ClassName> alloc]
. Variables declared in the global scope (that is, outside of any functions or class declarations) are just that: global. That means that the variable can only hold one value per running copy of your application.
在Objective-C中没有真正的私有变量,但你可以在编译时根据此处。
There are no truly private variables in Objective-C, but you can hide them from other instances at compile time as described here.
这篇关于UINavigationController崩溃,因为推和UIViewControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!