0

How would I limit the amount of text, that can be entered in a text field in a UIAlertView with my existing code?

I am new to iOS app development.

My code is as following:

if(indexPath.row== 1){
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Enter Novena Day"
                              message:@"Please enter the day of Novena:"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              otherButtonTitles:@"Ok", nil];
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *textField = [alertView textFieldAtIndex:0];
    textField.keyboardType = UIKeyboardTypeNumberPad;

    [alertView show];

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
John Doherty
  • 23
  • 1
  • 6
  • Implement the delegate of the text field, and implement code that checks how many characters there are and disallows any more than allowed. – Léo Natan Mar 22 '14 at 21:22
  • 1
    You limit it like you would for any other text field. – rmaddy Mar 22 '14 at 21:22
  • So I would be able to use a typical delegation for the text field? – John Doherty Mar 22 '14 at 21:26
  • 3
    @LeoNatan Really? Don't down vote a question just because the wrong tag is used, especially with a new user. Down vote for proper reasons such as an obvious lack of effort to research an answer first or failing to provide enough detail to provide help. – rmaddy Mar 22 '14 at 21:58
  • Is this correct way to delegate a textfield [textField setDelegate:shouldChangeCharactersInRange]; – John Doherty Mar 22 '14 at 22:05

3 Answers3

2

When you initialize the alert view:

[[alertView textFieldAtIndex:0] setDelegate:self];

Now, self here is your view controller. So you need to add <UITextFieldDelegate> to its declaration.

Now implement the delegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > self.maxAlertTextFieldLength) ? NO : YES;
}

This is taken from this answer, linked answer in the comments.

Community
  • 1
  • 1
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • There is an issue that has cropped up maxAlertTextFieldLength not found on object of type ViewController. – John Doherty Mar 22 '14 at 22:31
  • @JohnDoherty It's not found because you need to define a property and set it to the number of characters that you want to allow. My code is an example. – Léo Natan Mar 22 '14 at 22:32
0

i would suggest to better use a notification

check this

UITextField *textField = [alertView textFieldAtIndex:0];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldMaxLimit:) name:UITextFieldTextDidChangeNotification object:textField];


-(void)textFieldMaxLimit:(NSNotification*)notification
{
  UITextField *textField=(UITextField*)notification.object;

        if ([[textField text] length]>22) //choose your limit for characters
        {
            NSString *lastString=[[textField text] substringToIndex:[[textField text] length] - 1];;
            [textField setText:lastString];
        }
 } 
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • 1
    This approach is far from ideal and in fact doesn't work if the user pastes in a bunch of text (since you assume 1 character at a time). It's better to prevent the text from being added at all then adjusting it after the fact. – rmaddy Mar 22 '14 at 22:00
0

Implement shouldChangeCharactersInRange delegate , check your condition and return the desired value.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

  BOOL isValid = YES;

  @try
  {
     if(5 < [textField.text length])
     {
        isValid = NO;
     }
  }
  @catch (NSException *exception)
  {
     NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
  }
  @finally
  {
     return isValid;
  }
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • I am not sure I am delegating correctly, would you be able to check ` [textField setDelegate:shouldChangeCharactersInRange];` – John Doherty Mar 22 '14 at 21:42