gpt4 book ai didi

ios - 点击按钮时 UIAlertView 崩溃

转载 作者:行者123 更新时间:2023-12-03 04:29:15 26 4
gpt4 key购买 nike

我在使用普通的 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 发布得太早了。当我将其作为调用类的强大属性时,它工作得很好。但让一切都成为强大的属性(property)并不能解决问题,不是吗?使用 ARC 强存储局部变量的正确方法是什么?

最佳答案

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

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

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

所以基本上检查谁是警报委托(delegate)在提交后是否没有被释放。

希望对你有帮助

关于ios - 点击按钮时 UIAlertView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18161491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com