Thursday, April 21, 2011

Is it possible to NOT dismiss a UIAlertView

The UIAlertviewDelegate protocol has several optional methods including:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

This would seem to suggest that not all button clicks actually dismiss the alert view. However I see no way of configuring the alert view to NOT automatically dismiss with any button press.

Do I have to create a subclass to accomplish this?

Why would the UIAlertViewDelegate Protocol have:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

If it didn't optionally support not dismissing the alert view with each button click?

Brief Aside: I realize what UIAlertView was designed for. But my purpose is to allow the user to copy some text to the paste board before the app exits (which happens automatically when the alert view is dismissed.

From stackoverflow
  • Yes. Subclass UIAlertView and then overload -dismissWithClickedButtonIndex:animated:, e.g.

    @implementation MyAlertView 
    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
       if (buttonIndex should not dismiss the alert)
       [super dismissWithClickedButtonIndex:buttonIndex animated:animated];

    Unofficially you can define a

    -(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(id)button;

    method to the delegate which will make it bypass -dismissWithClickedButtonIndex:animated:, but it's undocumented, so I don't know whether it's suitable for you.

    Corey Floyd : Thats what I am doing now. But because of the available delegate methods, it seems like I shouldn't have to. Oh well…
    KennyTM : There's an undocumented method (not tested), see the edit.
    Corey Floyd : Hmm… interesting. Too bad they have crcked down on using undocumented APIs.
  • willPresentAlertView:, didPresentAlertView:, alertView:willDismissWithButtonIndex:, and alertView:didDismissWithButtonIndex: are for tracking the start and end of UIAlertView's animations.

    Applications that don't need to track UIAlertView's animations can simply use alertView:clickedButtonAtIndex:. The docs for that method say "the receiver is automatically dismissed after this method is invoked."

    Corey Floyd : Thanks, I missed that last piece of text in the docs. Still seems like overkill to have that extra method, since it essentially the same as willPresentAlertView…


Post a Comment