4

I'm running into a problem where 9 times out 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.

Vince
  • 73
  • 1
  • 5

4 Answers4

9

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.)

Dave Taubler
  • 1,081
  • 3
  • 12
  • 25
2

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

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 1
    Right, and that applies to most delegate methods except for this one. According to Apple's documentation, when you call dismissWithClickedButtonIndex:animated:, you provide a button index to be used for the delegate. Furthermore, this behavior only started happening with iOS 5; on iOS 4 and older, the delegate method works as intended when you programmatically dismiss the alert view. The other maddening thing is that sometimes it would work so I can't just insert a call to my alertView:didDismissWithButtonIndex: method because it'll get called twice. – Vince Oct 13 '11 at 15:05
1

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.

Community
  • 1
  • 1
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
0

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.

junjie
  • 7,946
  • 2
  • 26
  • 26