点击按钮时UIAlertView崩溃



我在使用普通的UIAlertView时遇到了一些问题。

场景:我的应用程序能够导入自己的数据类型(用于备份/还原)。在-application:openURL:sourceApplication:annotation:之后,我创建了导入程序类并开始导入操作本身。在实际导入开始之前,应该询问用户是否要删除/覆盖他的旧数据库。

UIAlertView显示得很好。当我按下两个按钮中的一个(YesNo)时,应用程序在控制台上没有任何信息的情况下崩溃。我正在提高主线的警惕。它是这样创建的:

UIAlertView *dataAvailableAlert = [[UIAlertView alloc] initWithTitle:alertTitle
                                                             message:alertMessage
                                                            delegate:self
                                                   cancelButtonTitle:noButtonTitle
                                                   otherButtonTitles:yesButtonTitle, nil];
[dataAvailableAlert show];

我的类采用了UIAlertViewDelegate并实现了-alertView:clickedButtonAtIndex:。后者根本没有接到电话更新:我正在使用ARC。回溯:

* thread #1: tid = 0x1c03, 0x01bc809f libobjc.A.dylib`objc_msgSend + 19, stop reason = EXC_BAD_ACCESS (code=1, address=0xfd940000)
    frame #0: 0x01bc809f libobjc.A.dylib`objc_msgSend + 19
    frame #1: 0x00eee0bc UIKit`-[UIAlertView(Private) _buttonClicked:] + 294
    frame #2: 0x01bca705 libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 77
    frame #3: 0x00afe2c0 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
    frame #4: 0x00afe258 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    frame #5: 0x00bbf021 UIKit`-[UIControl sendAction:to:forEvent:] + 66
    frame #6: 0x00bbf57f UIKit`-[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578
    frame #7: 0x00bbe6e8 UIKit`-[UIControl touchesEnded:withEvent:] + 546
    frame #8: 0x00b2dcef UIKit`-[UIWindow _sendTouchesForEvent:] + 846
    frame #9: 0x00b2df02 UIKit`-[UIWindow sendEvent:] + 273
    frame #10: 0x00b0bd4a UIKit`-[UIApplication sendEvent:] + 436
    frame #11: 0x00afd698 UIKit`_UIApplicationHandleEvent + 9874
    frame #12: 0x02779df9 GraphicsServices`_PurpleEventCallback + 339
    frame #13: 0x02779ad0 GraphicsServices`PurpleEventCallback + 46
    frame #14: 0x024b5bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
+ 53
    frame #15: 0x024b5962 CoreFoundation`__CFRunLoopDoSource1 + 146
    frame #16: 0x024e6bb6 CoreFoundation`__CFRunLoopRun + 2118
    frame #17: 0x024e5f44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #18: 0x024e5e1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #19: 0x027787e3 GraphicsServices`GSEventRunModal + 88
    frame #20: 0x02778668 GraphicsServices`GSEventRun + 104
    frame #21: 0x00afaffc UIKit`UIApplicationMain + 1211
    frame #22: 0x00002e8d Foo`main(argc=1, argv=0xbffff61c) + 141 at main.m:16
    frame #23: 0x00002db5 Foo`start + 53

知道这里出了什么问题吗?

谢谢
–f

更新:按要求更新整个类。

@implementation NEADatabaseImporter
- (id)initWithDictionary:(NSDictionary *)importDictionary {
    self = [super init];
    if (self) {
        _importDictionary = importDictionary;
    }
    return self;
}
- (void)startImport {
    // check if the current database already has some data in it
    BOOL dataCreated = ([[NEADataAccessor groups] count] > 1);
    // if so ask the user if he wants to delete the old data
    if (dataCreated) {
        // prepare variables for better reading
        NSString *alertTitle = NSLocalizedString(@"REPLACE_DATABASE_TITLE", @"The title for the alert asking the user to let the app replace his database upon import.");
        NSString *alertMessage = NSLocalizedString(@"REPLACE_DATABASE_MESSAGE", @"The message the alert displays when asking the user for permission to replace the database upon import.");
        NSString *yesButtonTitle = NSLocalizedString(@"YES", @"Yes");
        NSString *noButtonTitle = NSLocalizedString(@"NO", @"No");
        UIAlertView *dataAvailableAlert = [[UIAlertView alloc] initWithTitle:alertTitle
                                                                     message:alertMessage
                                                                    delegate:self
                                                           cancelButtonTitle:noButtonTitle
                                                           otherButtonTitles:yesButtonTitle, nil];
        [dataAvailableAlert show];
    } else {
        // if there was no data yet we can directly invoke the import
        [self removeDatabase];
        [self readDatabaseImportDictionary];
    }
}
- (void)removeDatabase {
    NEAAppDelegate *delegate = (NEAAppDelegate *)[[UIApplication sharedApplication] delegate];
    [[delegate databaseManager] clearStores];
}
- (void)readDatabaseImportDictionary {
    if (![self importDictionary]) return;
    // read it here
    NEAAppDelegate *delegate = (NEAAppDelegate *)[[UIApplication sharedApplication] delegate];
    [[delegate currentProfile] readDataDictionary:[self importDictionary]];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSInteger yesIndex = 1;
    if (buttonIndex == yesIndex) {
        // go ahead and replace the database
        [self removeDatabase];
        [self readDatabaseImportDictionary];
    } else {
        // cancel the import
        NSLog(@"import of dictionary canceled: %@", [self importDictionary]);
    }
}
@end

更新:shannoga是对的。看起来我的NEADatabaseImporter发布得太早了。当我使它成为调用类的强属性时,它工作得很好。但是,让一切都成为一个强大的属性不可能是解决方案,不是吗?使用ARC强存储局部变量的正确方法是什么?

这只是猜测,但几个月前我也遇到了同样的问题。

我的问题是,警报视图的委托在呈现后被释放,因此当警报被解除时,它会向其委托发送一条消息,但它已不存在。

另一个选项是,显示警报视图的viewController在显示后释放。

因此,基本上要检查谁是警报委托在出现后是否没有被释放。

希望它能帮助

最新更新