在uinavigationcontroller设置中防止popViewControllerAnimated的访问崩溃 [英] Preventing bad access crash for popViewControllerAnimated in uinavigationcontroller setup

查看:533
本文介绍了在uinavigationcontroller设置中防止popViewControllerAnimated的访问崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,在低内存下,我的根视图是卸载的。这是预期和解释。但是,我如何处理需要被弹出的模态/推送视图控制器,因为他们引用了主navigationController?我有一个自定义完成按钮在推视图控制器,调用[self.navigationController popViewControllerAnimated:YES]。如果根视图已经卸载,这将给出一个错误的访问错误。有没有更好的方法来做到这一点?



我的设置是
AppDelegate有一个NavigationController
这个NavigationController有一个视图控制器MainViewController

  // MainViewController.m 
- (IBAction)showAnotherController:(id)sender
{
AnotherViewController * anotherViewController;
anotherViewController = [[AnotherViewController alloc] initWithNibName:@AnotherViewbundle:nil];

[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
}

// ...我可以模拟一个内存警告强制卸载MainViewController的视图

//在AnotherViewController.m中,从一个自定义工具栏项
- (IBAction)done:(id)sender
{
[self.navigationController popViewControllerAnimated:YES]; //坏的访问在这里,看起来像self.navigationController不再可用。我做错了吗?

}


解决方案

你描述不会导致不良访问。但是,如果您不仔细处理所描述的方案,您会得到不好的访问。因为你没有给我任何代码,我不能告诉你,这是你遇到的问题,但这是一个很常见的问题。



一个内存警告,你的视图卸载,你可能会发布一堆东西。除了释放变量之外,还必须将它们设置为nil。如果你不这样做,你就面临着向发布对象发送消息,导致访问不良的风险。



同样,我不知道这是你的

编辑:因为你似乎相信 self.navigationController 不存在(它可能是)我会告诉你一些名为NSZombie的东西:



NSZombie会告诉你释放的对象被发送消息(也称为EXC_BAD_ACCESS)。这是一个非常有用的工具,当你得到EXC_BAD_ACCESS,所以学习如何使用它。



要激活NSZombie,请执行以下操作:


  1. 获取可执行文件的信息。

  2. 转到参数选项卡。

  3. 在环境中设置:部分添加:

名称:NSZombieEnabled
值:YES



然后像往常一样运行你的应用程序,当它崩溃时,它应该告诉你哪个被释放的对象收到了该消息。


So, under low memory, my root view is unloaded. That's expected and accounted for. However, how do I deal with modal/pushed view controllers who need to be popped, since they reference the main navigationController? I have a custom Done button in a pushed view controller, which calls [self.navigationController popViewControllerAnimated:YES]. If the root view has been unloaded, this gives a bad access error. Is there a better way to do this?

My setup is AppDelegate has a NavigationController this NavigationController has a view controller MainViewController

//MainViewController.m
- (IBAction)showAnotherController:(id)sender
{
    AnotherViewController *   anotherViewController;
    anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];

    [self.navigationController pushViewController:anotherViewController animated:YES];
    [anotherViewController release];
}

//...Here I can simulate a memory warning to force unloading of MainViewController's view

//in AnotherViewController.m, called from a custom toolbar item
- (IBAction)done:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES]; // bad access here, looks like self.navigationController is no longer available. Am I doing this wrong?

}

解决方案

The scenario you're describing does not cause bad access. However, if you don't handle the described scenario carefully you get bad access. Since you haven't showed me any code I can't tell you that this is the problem you're having, but it's a very common problem.

When you receive a memory warning and your view is unloaded, you probably release a bunch of stuff. In addition to releasing the variables you also have to set them to nil. If you don't, you're facing the risk of sending messages to released objects, which causes bad access.

Again, I can't know that this is your problem, but it's usually the problem.

EDIT: Since you seem to believe that self.navigationController doesn't exist (it probably does) I'm gonna tell you about something called NSZombie:

NSZombie will tell you what released object is being sent a message (aka EXC_BAD_ACCESS). This is a very useful tool when you get EXC_BAD_ACCESS, so learn how to use it.

To activate NSZombie do the following:

  1. Get info of the executable.
  2. Go to the arguments tab.
  3. In the "Variables to be set in the environment:" section add:

Name: NSZombieEnabled Value: YES

Then run your app as usual and when it crashes it should tell you which deallocated object received the message.

这篇关于在uinavigationcontroller设置中防止popViewControllerAnimated的访问崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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