views:

285

answers:

2

The UIAlertViewDelegate protocol defines two methods, alertView:clickedButtonAtIndex: and alertView:didDismissWithButtonIndex:, which seem to me to be identical in usefulness.

Why is there a clickedButtonAtIndex and a didDismissButtonWithIndex when they both do the same thing? I realize there is also a willDismissButtonWithIndex that happens before the alert view is dismissed, but is there any reason to use clickedButtonAtIndex instead of didDismissButtonWithIndex?

+5  A: 

Hi,

The "alertView:clickedButtonAtIndex:" is called when the user clicks a button on an alert view whereas the "alertView:didDismissWithButtonIndex:" is called after an alert view is dismissed from the screen. (see the UIAlertViewDelegate Protocol Reference)

The difference is minimal but it allows you to do something before or after animation.

Yannick L.
willDismissWithButtonIndex method looks completely identical to clickedButtonAtIndex: then...
Vladimir
Yes, but there is a little difference. One is call when the user has pushed a button (so the alert is always visible) and the other is call when the alert has disappeared.So, for example, if you want make a transition between 2 views it's better to do this when the alert has closed in the "alertView:didDismissWithButtonIndex:".
Yannick L.
But is there any reason to use clickedButtonAtIndex over willDismissWithButtonIndex (not didDismiss) or vice versa?
Ed Marty
You can use this method for tracking the start and the end of the UIAlertView animation. But I don't have any use case for that in my mind for the moment...
Yannick L.
+3  A: 

I found a more useful difference between the two:

When showing a UIAlertView, and the device goes to sleep, alertView:didDismissWithButtonAtIndex: gets called, even though the alert view is not actually dismissed. It is shown again once the device wakes up. alertView:clickedButtonAtIndex: is only called when the user clicks one of the buttons.

Ed Marty