如何防止后面的代码在 UIAlertController 中先执行
how to prevent later code getting executed first in UIAlertController
我正在使用 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 的值总是 returns 0 因为它在我响应之前被执行 UIAlertController
因为后面的代码首先被执行如何防止它。
如果您的 运行 代码本质上是异步的,那么从调用方法返回一个值将不会很好地工作(除非您设置了一些相当复杂的阻塞)。
最好的办法是让调用者提供委托或侦听通知,而不是依赖于返回值。这样,您就可以触发完成处理程序中需要发生的任何事情。
您可以创建一个带有完成块的函数,如下所示,并在需要的地方调用它,只需检查标志
- (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);
}
我正在使用 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 的值总是 returns 0 因为它在我响应之前被执行 UIAlertController
因为后面的代码首先被执行如何防止它。
如果您的 运行 代码本质上是异步的,那么从调用方法返回一个值将不会很好地工作(除非您设置了一些相当复杂的阻塞)。
最好的办法是让调用者提供委托或侦听通知,而不是依赖于返回值。这样,您就可以触发完成处理程序中需要发生的任何事情。
您可以创建一个带有完成块的函数,如下所示,并在需要的地方调用它,只需检查标志
- (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);
}