Dismissing UIAlertView programmatically
I have a UIAlertView with the following delegate method:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)开发者_运维问答buttonIndex {
But I have a problem. In that method, I do some logic and perform some lines of code which take a small amount of time. I would like to dismiss the alertView before I do all of this. I want to dismiss the alert view at the very top of that method. That way, when the user taps a button on the alertView, the app doesn't seem frozen for a second while the next lines of code are executed.
To dismiss a alert programaticly do:
/* Your Processing Code Here */
[theAlertView dismissWithClickedButtonIndex:0 animated:YES];
The [theAlertView dismissWithClickedButtonIndex:0 animated:YES]; will make the alert go away
Either use the delegate method -alertView:didDismissWithButtonIndex:
instead—it gets called once the alert view’s been removed from the screen, which will at least conceal the lag your app’s having—or, better, use a background thread, e.g. with -performSelectorInBackground:withObject:
, to handle whatever processing you need to do.
Use the other delegate method for did, instead of will:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
You should probably also execute the other lines of code you mention asynchronously, so as not to freeze the UI at all.
精彩评论