gpt4 book ai didi

ios/xcode : UIAlert Not stopping action and clicking OK crashing app

转载 作者:行者123 更新时间:2023-11-29 01:41:44 26 4
gpt4 key购买 nike

将某些内容保存到核心数据后,我想显示一条提醒以感谢用户。当用户单击“确定”时,我想关闭执行保存的模态视图 Controller 。

但是,警报并没有阻止 Controller 的关闭,并且当您点击“确定”时,还会导致应用程序崩溃。我的理解是,当 Controller 不再存在时,可能会发生这些崩溃。然而,在这种情况下,我会在关闭 Controller 之前启动警报。

谁能发现出了什么问题吗?

非常感谢任何建议。

代码:

if ([self.managedObjectContext save:&error]) {
[self fireAlert];
// Dismiss View Controller
[self dismissViewControllerAnimated:YES completion:nil];

} else {
if (error) {
NSLog(@"Unable to save record.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}

-(void) fireAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you!" message:@"We appreciate your feedback" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];
}

最佳答案

您正在关闭 View Controller ,因此 delegate:self导致崩溃,因为之后

[self dismissViewControllerAnimated:YES completion:nil];

self不再被提供。它已被释放并向已释放的对象发送消息导致崩溃。

尝试制作delegate:nil如果您不想使用 UIAlertView委托(delegate)方法。

或者使用委托(delegate)方法,确保你有UIViewController <UIAlertViewDelegate>

然后做

if ([self.managedObjectContext save:&error]) {
[self fireAlert];

} else {
if (error) {
NSLog(@"Unable to save record.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}

-(void) fireAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you!" message:@"We appreciate your feedback" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
// Dismiss View Controller
[self dismissViewControllerAnimated:YES completion:nil];
}

关于ios/xcode : UIAlert Not stopping action and clicking OK crashing app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32318260/

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