Make UIAlertView stay

157 views Asked by At

I have a UIAlertView that I implemented in viewDidLoad. I'm trying to make the alertView stay when the otherButton (buttonAtIndex:1) was selected. Here is my code:

UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Title"
                      message:@"Message:"
                      delegate:self cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"Done", nil];

[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) return;
    [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
}

When the second button was selected ("Done"), the alertView goes away. How can I make it stay?

2

There are 2 answers

2
Joride On BEST ANSWER

You should create your own alert view class that is NOT a subclass of UIAlertView. UIAlertView's documentation, it says under 'Subclassing notes:

The UIAlertView class is intended to be used as-is and does not support subclassing. (...)

Above referenced in UIAlertView Apple Documentation section marked Subclassing Notes

6
Thibaud David On

You might have what you want here :

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