iOS 7的错误还是UIAlertView出了问题?

29

当我在UIAlertView上点击“OK”时,我的应用程序崩溃并显示下面的堆栈跟踪。这是我的错还是iOS7的问题?我不知道如何修复它。

OS Version:          iOS 7.0 (11A465)
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000
Triggered by Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libobjc.A.dylib                 0x39d50b36 objc_msgSend + 22
1   UIKit                           0x3212e3da -[UIAlertView(Private) modalItem:shouldDismissForButtonAtIndex:] + 58
2   UIKit                           0x31ed2036 -[_UIModalItemsCoordinator _notifyDelegateModalItem:tappedButtonAtIndex:] + 90
3   UIKit                           0x31ed1f3e -[_UIModalItemAlertContentView tableView:didSelectRowAtIndexPath:] + 890
4   UIKit                           0x31dd7326 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1074
5   UIKit                           0x31e8a24e -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 210
6   UIKit                           0x31d3a96e _applyBlockToCFArrayCopiedToStack + 314
7   UIKit                           0x31cb246e _afterCACommitHandler + 426
8   CoreFoundation                  0x2f5141d2 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
9   CoreFoundation                  0x2f511b74 __CFRunLoopDoObservers + 280
10  CoreFoundation                  0x2f511eb6 __CFRunLoopRun + 726
11  CoreFoundation                  0x2f47cce2 CFRunLoopRunSpecific + 518
12  CoreFoundation                  0x2f47cac6 CFRunLoopRunInMode + 102
13  GraphicsServices                0x3417727e GSEventRunModal + 134
14  UIKit                           0x31d1ea3c UIApplicationMain + 1132
15  MyApp                           0x000d8e5e 0xcb000 + 56926
16  libdyld.dylib                   0x3a25dab4 start + 0

提示框代码

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error"
    message:[NSString stringWithFormat:@"Es ist ein Fehler aufgetreten: %@", [error localizedDescription]]
    delegate:self
    cancelButtonTitle:@"OK"
    otherButtonTitles:nil];
[av show];

而且我还没有定义委托方法。


1
你能展示一下用于显示警告视图的代码以及你编写的委托方法吗?谢谢。 - Fogmeister
正如@Fogmeister所说,你能提供你的警报代码和它的代理代码吗?谢谢! - matteodv
4个回答

17

我太蠢了,我只需要将警告视图的代理设置为nil


1
没错,我刚刚正在写关于那个的评论! :) - matteodv
7
但是,我仍然认为即使将delegate设置为self,它也不应该崩溃。如果UIAlertView的协议得到正确实现,它也应该能够与将delegate设置为self的情况一起正常工作。我错了吗? - Hans One
4
这是否意味着仅因为您设置了委托但未处理其委托方法,所以它只发生在您身上?我无法重现那种行为 - 我的应用程序不会崩溃,但是我在CrashLytics中得到了相同的崩溃日志... - swalkner
@swalkner 是的,它确实这样做。 - Hans One

13

因为 UIAlertView 的代理是一个赋值属性,我认为这是苹果公司的问题。它应该在 ARC 中是一个弱引用指针。但它是一个赋值属性,所以在销毁代理之前(大多数情况下是控制器类被弹出或回退),您需要将任何警报视图的代理设置为 nil。阅读 UIAlertView 的 .h 文件,关于代理,您可以发现它是一个赋值属性,有人在声明后添加了注释 "//弱引用"。


这正是发生的事情:委托在警告视图显示后立即被销毁了。 - Hans One
3
我也遇到了相同的问题,但只有在创建ipa并运行时才会出现,而在模拟器或调试中没有出现。为什么?有任何想法吗? - Johnykutty

5
为了避免在使用委托时出现UIAlertView的问题,最好将UIAlertView的实例作为委托类的iVar。然后在委托类的dealloc方法中将alertView的委托属性设置为nil。
@implementation YOUR_CLASS
{
    UIAlertView *_alert;
}

- (void)dealloc
{
    _alert.delegate = nil;
}


- (void)showAlertView
{
    _alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

相同的方法适用于所有具有分配类型委托的旧类。

0
如果您需要指定委托,对警报视图执行“autorelease”也可以起作用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接