问题
After iOS 8, changing my implementation to UIAlertController instead of UIAlertView is giving me some headaches.
The dismissal takes around a full second before the UI becomes responsive after clicking a button. It means users think there's something wrong.
Am I the only one suffering from this? It's across several apps, and true for an implementation as simple as this. I tried this in a new blank project.
- (IBAction)showAlertView {
[[[UIAlertView alloc] initWithTitle:@"test" message:@"test" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"this is fast");
}
-(IBAction)showAlert {
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"test" message:@"test" preferredStyle:UIAlertControllerStyleAlert];
[controller addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"this is slow");
}]];
[self presentViewController:controller animated:NO completion:nil];
}
Clicking the button gives around a 1-second delay before anything is printed to the console. Presentation of the alert is not delayed at all.
Edit: By closer timing it's probably more like 700ms, so not a full second, but still way too long for something that should be instantaneous.
回答1:
I know it's an old question but I had the same problem and adding this in the ViewController presenting the UIAlertController solved it for me:
- (BOOL)canBecomeFirstResponder {
return YES;
}
Swift version:
override func canBecomeFirstResponder() -> Bool {
return true
}
回答2:
Sadly, overriding canBecomeFirstResponder
did not help us.
What did help us was to call resignFirstResponder
on any possible responders in the view controller BEFORE showing the alert.
For some reason, if a text field was a first responder, it looks like it would try to go through the view hierarchy looking for the next responder. If no text fields had any focus, the delay disappeared.
来源:https://stackoverflow.com/questions/37165632/uialertcontroller-dismiss-is-slow