简体   繁体   English

UIAlertView取消按钮使应用程序崩溃

[英]UIAlertView cancel button crashes app

Okay, so I have just started iOS development. 好的,所以我才刚刚开始iOS开发。 I'll start by explaining the flow of my app : 我将从解释我的应用程序流程开始:
1. A view called "appViewController" is loaded. 1.加载了一个名为“ appViewController”的视图。
2. [self presentViewController: controller animated: YES completion:nil]; 2. [self presentViewController: controller animated: YES completion:nil]; this loads a webview 这会加载一个webview
3. After I am done with the webview, I dismiss it and load a new UINavigation this way : 3.完成webview后,我将其关闭并以这种方式加载新的UINavigation:

[self dismissViewControllerAnimated:YES completion:^{
        formViewController *fv = [ [formViewController alloc] init ];
        UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:fv] autorelease];
        navController.navigationBar.hidden = YES;
        [presentingViewController presentModalViewController:navController animated:YES];

    }];

5.The formViewController has a button, which has the event attached to it for that display an alert this way 5. formViewController有一个按钮,该按钮具有附加的事件,该事件以这种方式显示警报

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                           message:@"test"
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
[av show];

Everything works as intended up till here. 到现在为止,一切都按预期进行。 Now when I click the "Ok"(Cancel) button, the app crashes with NSZombieEnabled saying 现在,当我单击“确定”(取消)按钮时,应用程序崩溃,并显示NSZombieEnabled
-[appViewController _deepestDefaultFirstResponder]: message sent to deallocated instance 0x6e6a570 lldb -[appViewController _deepestDefaultFirstResponder]:消息发送到已释放实例0x6e6a570 lldb

What is happening here? 这是怎么回事 Why is it trying to send message to appViewController again? 为什么尝试再次将消息发送到appViewController? There is no code after [av show] [av show]之后没有代码

NOTE : I am using ARC 注意:我正在使用ARC

If you're using arc, the autorelease in your code isn't valid. 如果您使用的是arc,则代码中的自动发布无效。

This seems like your root view controller gets deallocated at some point and when the responder chain gets traversed the resulting dangling pointer gets accessed. 看来您的根视图控制器在某个时候被释放了,并且当响应者链被遍历时,生成的悬空指针就被访问了。

To verify this, I would implement the dealloc method on appViewController and see if it gets called. 为了验证这一点,我将在appViewController上实现dealloc方法,看看它是否被调用。

dealloc {
  NSLog(@"Problems ahead.");
}

If this does get called before you'd expect that to happen (for a root view controller probably not at all), you need to find out why this happens. 如果在您希望发生这种情况之前确实调用了该方法(对于根视图控制器可能根本没有),则需要找出发生这种情况的原因。 You're probably missing a strong reference somewhere. 您可能在某处缺少强大的参考。 Check you app delegate and verify that you have a strong reference to the window and that you are setting your controller as the root view controller (provided you're not using storyboards). 检查您的应用程序委托,并确认您对该窗口有很好的引用,并且将您的控制器设置为根视图控制器(前提是您未使用情节提要)。

The Zombies instrument is very good for debugging such problems. 僵尸仪器非常适合调试此类问题。 It will list all retains and releases of your problematic object. 它将列出您有问题的对象的所有保留和发布。 Here's a short introduction to it . 这是它的简短介绍

A couple of things. 有几件事。

  1. You are mixing presentViewController and presentModalViewController. 您正在混合presentViewController和presentModalViewController。 If you are new to iOS, you should always use presentViewController. 如果您不熟悉iOS,则应始终使用presentViewController。 No need getting used to using a method that will soon be deprecated (see answer to this question Difference between presentModalViewController and presentViewController? 无需习惯使用即将弃用的方法(请参见此问题的答案presentModalViewController和presentViewController之间的区别?

  2. In general, you shouldn't autorelease unless you absolutely have to. 通常,除非绝对必要,否则您不应自动释放。 Since when a controller is presented with presentViewController (and presentModalViewController), it is retained, you can readily release afterwards. 由于向控制器提供presentViewController(和presentModalViewController)时,将保留该控制器,因此以后可以轻松释放它。 I would restructure like so: 我将这样重组:

 UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fv]; navController.navigationBar.hidden = YES; [presentingViewController presentModalViewController:navController animated:YES]; [navController release]; 

3.. Where is the section of code you included located? 3 ..您包括的代码部分在哪里? Like, you push a button and the view is dismissed or what? 就像,您按一个按钮,视图就消失了还是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM