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

问题描述 投票:0回答:4

当我的应用程序在 UIAlertView 上点击“确定”时崩溃时,我得到了以下堆栈跟踪。是我的错还是iOS7的bug?我不知道如何解决这个问题。

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];

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

ios objective-c ios7 uialertview
4个回答
17
投票

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


13
投票

因为

UIAlertView
的委托是
UIAlertView
中的一个赋值属性。我认为这是苹果的错。应该是ARC中的弱指针。但它是一个分配属性,因此您需要在销毁委托之前将任何警报视图的委托设置为
nil
(大多数情况下控制器类会弹出或导航回来)。阅读关于委托的
UIAlertView
的.h文件,你可以发现它是一个赋值属性,并且有人在声明后评论说“//弱引用”。


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
投票

如果您需要指定委托,在警报视图上执行“自动释放”也可以。

© www.soinside.com 2019 - 2024. All rights reserved.