1

我遇到了一些普通的问题UIAlertView

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

表现得UIAlertView很好。当我点击两个按钮之一(YesNo)时,应用程序崩溃,控制台上没有任何信息。我在主线程上发出警报。它是这样创建的:

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

我的班级采用UIAlertViewDelegateand implements -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强烈存储局部变量的正确方法是什么?

4

1 回答 1

9

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

我的问题是警报视图的代表在呈现后被释放,所以当警报被解除时,它会向它的代表发送一条消息,但它不再存在。

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

所以基本上检查谁是警报代表在出现后是否没有被释放。

希望它会有所帮助

于 2013-08-10T15:38:36.747 回答