dismissViewControllerAnimated:completion:
在应用中运行正常,就是不知道为什么出现了几秒钟的延迟:
[api loginWithUsername:[dict objectForKey:@"username"] andPassword:[dict objectForKey:@"password"] andSuccessBlock:^(id json) { NSLog(@"DONE... %@", [json objectForKey:@"status"]); NSString *status = [json objectForKey:@"status"]; if([status isEqualToString:@"ok"]){ app.user = [json objectForKey:@"data"]; [self dismissViewControllerAnimated:YES completion:nil]; }else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"could not log you in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } }];
在控制台可以看见DONE... ok。,意味着回调block执行正常,但是接下来需要三四秒钟延迟视图才能最终被dismiss。
怎么解决这个延迟问题?谢谢
如果你不能保证UI代码运行在主线程中,它可能就运行到其他地方去了,这样的话会出现几秒钟的延迟。
添加下面的代码可以让dismissal运行在主线程中:
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil]
});