iOS 7错误或我在UIAlertView中的错误

Han*_*One 29 objective-c uialertview ios7

当我的应用程序在UIAlertView上点击"确定"时崩溃时,我有以下堆栈跟踪.这是我的错还是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
Run Code Online (Sandbox Code Playgroud)

警报视图代码

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

我还没有定义委托方法.

Han*_*One 17

愚蠢的我,我只需要将警报视图的委托设置为nil

  • 但是,我仍然认为即使委托人设置为自己也不会崩溃.如果UIAlertView的协议正确实现,它也应该与委托设置为self.我错了吗? (7认同)
  • 这是否意味着它只发生在您身上,因为您已设置委托但不处理它的委托方法?我无法重现那种行为 - 我的应用程序没有崩溃,但我在CrashLytics中有相同的崩溃日志... (4认同)

小智 13

因为它的委托UIAlertView是一个赋值属性UIAlertView.我认为这是Apple的错.它应该是ARC中的弱指针.但它是一个assign属性,因此您需要nil在销毁委托之前设置任何警报视图的委托(大多数时候控制器类被弹出或导航回来).阅读UIAlertView关于委托的.h文件,你会发现它是一个assign属性,有人在声明后说"//弱引用".

  • 我遇到了同样的问题,但只有我们创建了一个ipa并运行它.不在模拟器或调试中为什么?任何的想法? (3认同)

mal*_*lex 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];
}
Run Code Online (Sandbox Code Playgroud)

对于具有assign-type委托的所有旧类,相同的方法已经足够了.