0

I'm creating a text input uialertview using the method below.

[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];

How can I limit the number of characters the user is able to input?

rmaddy
  • 307,833
  • 40
  • 508
  • 550
user1542125
  • 553
  • 5
  • 15

2 Answers2

3

Try this code

First add UITextFieldDelegate

UIAlertView *dialog = [[UIAlertView alloc]init];// Setup your alert 
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog textFieldAtIndex:0].placeholder = @"";//add this if you need
[dialog textFieldAtIndex:0].delegate = self;

and add textfield delegate as per Check this Answer

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

Maybe it will help you.

Community
  • 1
  • 1
ChintaN -Maddy- Ramani
  • 5,096
  • 1
  • 26
  • 47
0

If you have instance of your alertView just do the following

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if([self.alertView textFieldAtIndex:0])
    {
      NSUInteger newLength = [textField.text length] + [string length] - range.length;
      return (newLength > 25) ? NO : YES;
    }
    return YES; // suppress warning about control reaching end of non-void function
}
Zev Eisenberg
  • 7,970
  • 5
  • 35
  • 78
Pancho
  • 4,001
  • 1
  • 20
  • 32