点击按钮时UIAlertView崩溃

时间:2013-08-10 11:40:33

标签: ios delegates crash 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强烈存储局部变量的正确方法?

1 个答案:

答案 0 :(得分:9)

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

我的问题是警告视图的代表在提交后被释放,因此当警报被解除时,它会向其代理发送消息,但它不再存在。

另一个选择是,呈现警报视图的viewController在呈现后会被释放。

因此,基本上检查谁是警报代表是否在呈现后才被释放。

希望它会有所帮助