问题
I'm trying to change my UIAlertViews to UIAlertControllers. I set up this action for it:
UIAlertAction *undoStopAction = [UIAlertAction actionWithTitle:@"Undo Stop"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self undoStop];
}];
But, the handler doesn't run until about a second after the action is tapped. Is there any way to speed this up?
回答1:
The short delay is normal for Alert View (less than a second though). If this is not convenient for you, you can programmatically create a view that covers the screen with a label and a button, basically a customized alert view.
回答2:
There is a semi-solution here:
The handler of a UIAlertAction is a bit too late - how can I make it immediate?
Basically, subclass the alert controller and run a handler on viewWillDisappear. You can store the handler block on your alert controller.
The problem with this solution is that you can't run a handler per button like you can if you add action(s) with UIAlertAction. So that's a major limitation.
However, this solution worked for me (because I was trying to add a background view behind the alert when the alert appeared and make it animate away with the alert when the alert disappeared...originally I tried to do it with the button tap, but that didn't work because of the handler being delayed).
回答3:
As implied by the comment left by Mr. T, it's possible that the handler is not being called on the main thread, which could explain the delay. Try embedding your handler code in :
dispatch_async(dispatch_get_main_queue()) {
<your code>
}
and see if you still get the delay.
来源:https://stackoverflow.com/questions/32441856/uialertaction-handler-running-after-delay