2

I have a UIAlertView with a textField on it and two buttons: Save & Cancel. When the Save button is tapped I am checking if the text field isn't empty and after if it is I simply want to change the textFields placeholder to: @"enter a name please" and KEEP the alert view on screen. However it is automatically dismissed.

How do I override that?

Sergey Katranuk
  • 1,162
  • 1
  • 13
  • 23
  • 1
    Check this answer here for a better way to handle your situation: http://stackoverflow.com/questions/1947783/prevent-uialertview-from-dismissing – jonkroll Feb 08 '12 at 20:23
  • Seems you have to subclass to override the default dismiss behaviour, see: [http://stackoverflow.com/questions/2051402/is-it-possible-to-not-dismiss-a-uialertview](http://stackoverflow.com/questions/2051402/is-it-possible-to-not-dismiss-a-uialertview) – GregularExpressions Feb 08 '12 at 21:41
  • Thanks guys, but as I've written below to Brendan, I've decided to make my own alert view. Thanks for taking your time and commenting :) – Sergey Katranuk Feb 09 '12 at 20:33

1 Answers1

6

Add a target to the textfield in a subclassed alertView. You can subclass the alertView and not dismiss as described in this post

[[alertView textFieldAtIndex:0] addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];

Then write a function called textFieldDidChange that checks the current textfield of your alertView and set a boolean value so you know whether or not to dismiss the alert.

- (void) textFieldDidChange
{
  NSString *alertViewText = [[alertView textFieldAtIndex:0] text];

  if ([alertViewText isEqualToString:@""]) {
    [alertView setMessage:@"Enter a name please."];
  } else {
    [alertView setMessage:@"Default Message"];
  }
}

* Alternatively, I would suggest disabling "Save" when it is empty and not have to subclass. *

- (void) textFieldDidChange
{
  NSString *alertViewText = [[alertView textFieldAtIndex:0] text];

  if ([alertViewText isEqualToString:@""]) {
    [alertView setMessage:@"Enter a name please."];
    for (UIViewController *view in alertView.subview) {
       if ([view isKindOfClass:[UIButton class]]) {
          UIButton *button = (UIButton *)view;
          if ([[[button titleLabel] text] isEqualToString:@"Save"])
             [button setEnabled:NO];
       }      
    }
  } else {
    [alertView setMessage:@"Default Message"];
    for (UIViewController *view in alertView.subview) {
       if ([view isKindOfClass:[UIButton class]]) {
          UIButton *button = (UIButton *)view;
          if ([[[button titleLabel] text] isEqualToString:@"Save"])
             [button setEnabled:YES];
       }      
    }
  }
}
Community
  • 1
  • 1
brendan
  • 1,705
  • 1
  • 18
  • 24
  • Whoa, thanks for the reply Brendan. I thought it would be much easier. So, I've decided to make an AlertView of my own from scratch. Thanks again! :) – Sergey Katranuk Feb 09 '12 at 20:31
  • I currently have the alternative approach implemented in an app which is not that hard and gives you a lot of power. For example, you could disable the "Save" button when the user has entered ANY unallowed name. This could include an empty string, a name already in use or a banned name (such as a vulgar word or keyword). It also makes it so that the alertView:willDismissWithButtonIndex does not have to do any error checking as the user can only hit "Cancel" for an invalid name. – brendan Feb 09 '12 at 21:00
  • Yeah, I get that, it's just that I've started implementing custom made notifications in my app and I've decided that I need to majorly redesign the AlerViews as well. And that will be more work and more complicated for me then modifying the native alerts – Sergey Katranuk Feb 09 '12 at 21:03
  • By the way, can you help with how to programmatically make the background darken, like when an alert view appears? – Sergey Katranuk Feb 09 '12 at 21:04
  • 1
    I actually have something like this in an app. What you can do is add an imageView or a button that is set to an image of all black. You start off with its alpha set to 0 (you can do this in IB). When you want it to appear you set its alpha to something around .5 so it gets a grayed out effect. I also add animation effects so that it doesn't just suddenly appear. `[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:.2]; [blackWhatever setAlpha:.5]; [UIButton commitAnimations];` I have my darkened image as a button so that the user can click to dismiss. – brendan Feb 09 '12 at 23:01
  • Yeah, I thought of something similar, just not sure how well it will play with my custom alert. Well, I think it's worth a shot. Thanks for the advices, you've been very helpful :) – Sergey Katranuk Feb 10 '12 at 18:54