0

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?

Jessica
  • 9,379
  • 14
  • 65
  • 136

2 Answers2

2

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

Popeye
  • 11,839
  • 9
  • 58
  • 91
Joride
  • 3,722
  • 18
  • 22
  • Yes totally this is exactly one valid way of doing it. I honestly can't believe that two answers on here are telling the OP to Subclass `UIAlertView` clearly they haven't read the Apple Documentation +1. PS I added like to reference I hope you don't mind – Popeye Jun 12 '15 at 11:20
  • Thanks for adding the reference! – Joride Jun 12 '15 at 13:57
  • I just find it best to add the document you are referencing from then people can't question it or argue about it with you. – Popeye Jun 12 '15 at 13:59
-1

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
Community
  • 1
  • 1
Thibaud David
  • 496
  • 2
  • 11
  • Same as other answer just no such a bad answer. Have you even read the Apple Documentation in regards to `UIAlertView`s? You should never subclass `UIAlertView` please read the apple docs https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html and look at the section marked **Subclassing Notes** if you miss it though here it is `The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified` OP This is a bad answer never subclass `UIAlertView` – Popeye Jun 12 '15 at 11:19
  • Instead of "can't believe that two answers on here are telling the OP to Subclass", could you explain why Apple doesn't recommend that ? Furthermore, that doesn't modify the view hierarchy, does it ? – Thibaud David Jun 12 '15 at 11:29
  • read the documentation it clearly states `The UIAlertView class is intended to be used as-is and does not support subclassing` under the **subclassing notes** section. It is self explanatory, what don't you understand by it? – Popeye Jun 12 '15 at 11:31
  • You don't answer my question. I read the doc and saw that, I'm just asking WHY we shouldn't, particularly if we don't modify the view hierarchy (except the fact that Apples tells not to do it) – Thibaud David Jun 12 '15 at 11:33
  • Subclassing and altering the view hierarchy are two completely different thing. Altering the view hierarchy you are adding views to it by either using the `addSubview:` method or using the `setValue:forKey:@"accessoryView"` as for why you aren't allowed is beyond my knowledge and probably anyones on here so ask apple. As for subclassing which is being done in both answers this is just a no no as far as developers are concerned if you want to know why ask Apple again. Both though are classed as using a private API and would get rejected for that reason. – Popeye Jun 12 '15 at 12:08
  • I'd also have a look at the answer I wrote here for a little better description on why http://stackoverflow.com/questions/16119132/uialertview-displays-textboxes-and-buttons/30774349#30774349 – Popeye Jun 12 '15 at 12:09