gpt4 book ai didi

ios - 如何防止后面的代码在 UIAlertController 中先执行

转载 作者:行者123 更新时间:2023-11-28 21:26:46 24 4
gpt4 key购买 nike

我正在使用 shouldPrepareForSegue 方法,但我遇到了问题

__block BOOL Stat;

if([identifier isEqualToString:@"SignOut"]){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Are You Sure?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *can = [UIAlertAction actionWithTitle:@"CANCEL" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
Stat = NO;
[alert dismissViewControllerAnimated:YES completion:^{}];
}];
UIAlertAction *sign = [UIAlertAction actionWithTitle:@"SIGN OUT" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
Stat = YES;
NSLog(@"%d",Stat);
}];

[alert addAction:can];
[alert addAction:sign];
[self presentViewController:alert animated:YES completion:^{}];

NSLog(@"%d",Stat);
return Stat;

}else{
return YES;
}

Stat 的值总是返回 0,因为它在我响应 UIAlertController 之前被执行,因为后面的代码首先被执行如何防止它。

最佳答案

你可以像下面这样创建一个带有完成 block 的函数,并在你需要的地方调用它并检查标志

- (void)signOutWithcompletionHandler:(void (^)(BOOL flag))completionHandler

if([identifier isEqualToString:@"SignOut"]){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Are You Sure?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *can = [UIAlertAction actionWithTitle:@"CANCEL" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
completionHandler(NO);
[alert dismissViewControllerAnimated:YES completion:^{}];
}];
UIAlertAction *sign = [UIAlertAction actionWithTitle:@"SIGN OUT" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
completionHandler(YES);
NSLog(@"%d",Stat);
}];

[alert addAction:can];
[alert addAction:sign];
[self presentViewController:alert animated:YES completion:^{}];
}else{
completionHandler(YES);
}

关于ios - 如何防止后面的代码在 UIAlertController 中先执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836308/

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