AlertView点击确定后再执行后面的代码

AlertView的show方法执行后,后面的代码会继续运行,而不会等待用户按键结束再走,这样,如果把弹出的代码写在一个BOOL函数里,没等用户确认直接返回NO就惨了,解决方法:

 1 - (BOOL)beforeBackButtonClickEvent {
 2     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"舍弃流程?" delegate:self cancelButtonTitle:@"" otherButtonTitles:@"", nil];
 3     [alert show];
 4     CFRunLoopRun();
 5     
 6     return back;
 7 }
 8 
 9 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
10     //
11     if (buttonIndex == 0) {
12         back = NO;
13     }
14     //
15     else {
16         back = YES;
17     }
18     CFRunLoopStop(CFRunLoopGetMain());
19 }
原文地址:https://www.cnblogs.com/Steak/p/3972734.html