开发者

Programmatically dismissing a UIAlertView on iOS 5 doesn't call didDismiss delegate method

开发者 https://www.devze.com 2023-04-12 12:26 出处:网络
I\'m running into a problem where 9 times out开发者_StackOverflow of ten, when I call UIAlertView\'s dismissWithClickedButtonIndex:animated:, the delegate method alertView:willDismissWithButtonIndex:

I'm running into a problem where 9 times out开发者_StackOverflow of ten, when I call UIAlertView's dismissWithClickedButtonIndex:animated:, the delegate method alertView:willDismissWithButtonIndex: is not called. Is anyone else running into this problem? I'm about to file a bug with Apple but I'm curious to see if anyone else has run into this issue and figured out any workarounds.


To ensure a consistent behavior across iOS4 and 5, you could just remove the UIAlertView's delegate just prior to calling its dismissWithClickedButtonIndex:animated: method, then manually invoke the delegate method. e.g.

- (void)somethingDidHappen {
    id<UIAlertViewDelegate> delegate = myAlertView.delegate;
    myAlertView.delegate = nil;
    // now, we know the delegate won't be called...
    [myAlertView dismissWithClickedButtonIndex:0 animated:NO];
    // ...so we call it ourselves below
    [delegate alertView:myAlertView clickedButtonAtIndex:0];
}

(That code isn't tested, but you get the point.)


Delegates of UI objects are only called when the user performs an action. Apple assumes that when you do something from code, you already know what you're doing and you don't need to be informed. That applies to all delegates (scrolling delegate methods of UIScrollView vs. code-scrolling, Table View manipulation, ...)
Anyway, what button index should the delegate be called with?.. there is no one when you dismiss programmatically


According to Why doesn't dismissWithClickedButtonIndex ever call clickedButtonAtIndex? the problem is that a different method is being called. However, that doesn't explain why you get erratic calls. On the devices I tested the dismiss method gets called correctly, so I only redirect it to the click version.

Maybe you should file a bug with Apple if you continue seeing the erratic behaviour.


There are alertView:clickedButtonAtIndex:, alertView:didDismissWithButtonIndex: and alertView:willDismissWithButtonIndex:. The method that you're referring to (clickedButtonAtIndex:) is only called when the user explicitly taps on a button on your alert view (hence 'clicked').

Programmatic calls via dismissWithClickedButtonIndex:animated: to dismiss the alert does not seem to call alertView:clickedButtonAtIndex:.

So, if you need some behavior to be always triggered upon the dismissal of the alert view—whether it was triggered by the user tapping on a button or triggered programmatically—then using the didDismissWithButtonIndex: and willDismissWithButtonIndex: makes more sense.

0

精彩评论

暂无评论...
验证码 换一张
取 消