在iPhone顶级异常处理程序中显示警报 [英] Showing an alert in an iPhone top-level exception handler

查看:124
本文介绍了在iPhone顶级异常处理程序中显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在顶级的iPhone异常处理程序中显示一个UIAlertView。处理函数如下所示:

  void applicationExceptionHandler(NSException * ex){
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@Error
message:[ex reason]
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alertView show];
}

我在其他地方看过类似的代码(例如NSSetUncaughtExceptionHandler没有捕获iPhone上的所有错误)。



如果我在调试器中单步执行我可以看到异常处理程序被调用,我可以看到当前的屏幕变暗,好像它会在其前面显示警报,但没有任何显示。在调试器外面,应用程序刚刚退出并返回到系统主屏幕。



如果我在applicationDidFinishLaunching中捕获错误并在那里显示警报,它将工作返回。我假设警报视图从来没有机会在异常处理程序中显示,因为应用程序正在终止(相对于坐在那里,如果我刚刚退出applicationDidFinishLaunching,则无法执行)。有没有办法使这项工作?

解决方案

我不知道如何 [alertView show ] 被实现,但是我想象它会对视图层次结构进行一些更改,然后设置自己以在下一遍遍历运行循环中显示警报(查找 NSRunLoop )。



但是,由于应用程序即将退出,控件不会返回到运行循环,因此警报永远不会显示。这就是为什么你看到屏幕变暗(警报级UIWindow立即被添加到 show ),但是警报不会出现(这将在运行循环中发生) / p>

如果在异常处理程序结束时包含 [[NSRunLoop currentRunLoop] run] ,则可能会显示警报。



如果您想让您的应用程序在警报完成后退出,您可以通过调用NSRunLoop的 runUntilDate来执行此操作:在一个while循环中,检查一个标志的值,看看是否已经关闭警报。如果它有,只需退出处理程序函数,你很好去。这意味着你必须在设置该标志的警报上设置一个委托对象。



如果你想让你的应用程序继续运行...那里我不太确定您可以让运行循环继续用尽异常处理程序,但是可能会有不良/奇怪的副作用。所以你可能应该让应用程序退出。此外,如果你确定你可以从异常中恢复,你应该在某个地方找到它。


I'm trying to display a UIAlertView in a top-level iPhone exception handler. The handler function looks like this:

void applicationExceptionHandler(NSException *ex) {
  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                      message:[ex reason]
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];
  [alertView show];
}

I've seen similar code elsewhere (for instance, NSSetUncaughtExceptionHandler not catch all errors on iPhone).

If I single-step in the debugger, I can see that the exception handler is called, and I can see the current screen dim as if it's going to display the alert in front of it, but nothing appears. Outside of the debugger, the app just quits immediately and goes back to the system home screen.

It does work if I trap an error in applicationDidFinishLaunching and display an alert there before returning. I assume that the alert view never gets a chance to display in the exception handler because the app is terminating (as opposed to sitting there doing nothing if I just bail out of applicationDidFinishLaunching). Is there a way to make this work?

解决方案

I don't know exactly how [alertView show] is implemented, but I imagine it makes some changes to the view hierarchy and then sets itself to display the alert on the next pass through the run loop (look up NSRunLoop).

But, since the app is about to quit, control doesn't return to the run loop, so the alert is never displayed. That's why you see the screen dim (the alert-level UIWindow is immediately added by show) but the alert doesn't appear (that would happen in the run loop).

If you include [[NSRunLoop currentRunLoop] run] at the end of your exception handler, the alert may appear.

If you want to let your app quit once the alert is done, you can probably do so by calling NSRunLoop's runUntilDate: in a while-loop, checking the value of a flag to see if the alert has been dismissed yet. If it has, simply exit the handler function and you're good to go. That means you'll have to set a delegate object on the alert which sets that flag.

If you want to let your app continue running... there I'm not so sure. You could just let the run loop continue to run out of the exception handler, but there might be bad/strange side effects to that. So you probably should let the app quit. Besides, if you're sure you can recover from the exception, you should have caught it somewhere.

这篇关于在iPhone顶级异常处理程序中显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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