views:

960

answers:

2

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

AND

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

+2  A: 

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)
      return;
   [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
@end

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.

KennyTM
Thats what I am doing now. But because of the available delegate methods, it seems like I shouldn't have to. Oh well…
Corey Floyd
There's an undocumented method (not tested), see the edit.
KennyTM
Hmm… interesting. Too bad they have crcked down on using undocumented APIs.
Corey Floyd
+2  A: 

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

Darren
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…
Corey Floyd