当使用Init,PushViewController,Release时奇怪的Exc错误访问。这段代码有什么问题?
也许我一直在寻找这个太久;)我的应用程序有一个NavigationController和几个ViewControllers。从ViewController的两个级别downView(mainViewController),从rootViewController加载,我有下面的代码。在PushViewController到dataViewController后面(例如按下按钮)后,应用程序崩溃。当使用Init,PushViewController,Release时奇怪的Exc错误访问。这段代码有什么问题?
dataViewController加载得很好,但是当轻击navigationController的后退按钮时,应用程序会与对象异常一起崩溃。如果我删除:
[dataViewController release];
该应用程序工作正常。这很奇怪,因为dataViewController是以相同的方法初始化的。 任何想法?
- (void) locationPage
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotifyRemoveMap" object:nil];
MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
UINavigationController *navigation = app.navigationCantroller;
[navigation popToRootViewControllerAnimated:NO];
DataViewController *dataViewController = [[DataViewController alloc] initWithNibName:@"DataView" bundle:nil];
[dataViewController setCategoryId:category];
MyLanguage *lang = app.lang;
Mylocation *location = [lang locationForCategoryId:category];
dataViewController.title = location.name;
NSArray *locationArray = [lang locations];
dataViewController.locations = locationArray;
[navigation pushViewController:dataViewController animated:YES];
[dataViewController release]; // With this removed, app doesn't crash
}
甚至没有阅读您的文章。如果是Exec-Bad-Access,我有2个词给你:
启用NSZombies。
请点击此链接:(它解释了你需要知道的修复任何坏的访问问题的一切)
Phone Memory Debug with NSZombie and Instruments
干杯!
这个问题可能出现在dataViewController被弹出并且你试图访问它的东西时 - 它已经被释放了。您可以查看控制台了解更多细节 - 更好的是,以调试模式运行(调试配置和与调试器一起运行)。
您可以编辑您的问题以显示与后退按钮一起运行的一些代码。
你说的是释放dataViewController
,但你的代码是detailsViewController
。你是否复制并粘贴不正确,或者是错误?
您应该考虑不要使用app.navigationController
而是self.navigationController
。清洁设计。对应用程序委托的依赖性较小,常常被用作知道太多的frankenstein对象。
宾果! Lupu1001,你的回答挽救了我的生命! ;) 我喜欢你!!! – Jordan 2010-09-11 04:45:17
很高兴我能帮到你。回头见。 – 2010-09-12 23:56:49