0

I have a textfield in the alertview called phoeNumberTF.

phoneNumberTF.delegate = self;

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter your phone number" message:nil preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction
                  actionWithTitle:@"Send"
                  style:UIAlertActionStyleDefault
                  handler:^(UIAlertAction * action)
                  {
                      NSArray * textfields = alert.textFields;
                      phoneNumberTF = textfields[0];
                      [self sendPhoneNumber:phoneNumberTF.text];
                  }]];

shouldChangeCharactersInRange delegate method is not getting called

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

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
casillas
  • 16,351
  • 19
  • 115
  • 215
  • No, you do not have a text field in the alert controller. `phoneNumberTF` has nothing to do with the alert. – rmaddy Apr 06 '18 at 04:10
  • then why it does not work? – casillas Apr 06 '18 at 04:12
  • The code you have posted does not add a text field to the alert controller. Is there more code you are not showing? – rmaddy Apr 06 '18 at 04:13
  • @rmaddy if he doesn't have text field, he will got crash when using `phoneNumberTF = textfields[0];` not only the method isn't called. Maybe he add textField but didn't write it on the question. If you give my answer down vote, consider your decision – trungduc Apr 06 '18 at 04:13
  • @rmaddy Understood and removed my answer. Thank you. – trungduc Apr 06 '18 at 04:16
  • See https://stackoverflow.com/questions/39246989/how-to-check-the-textfiled-input-string-when-user-inputting – rmaddy Apr 06 '18 at 04:24

1 Answers1

2

phoneNumberTF is not a part of UIAlertController so no need of delegate for this phoneNumberTF.delegate = self;

add the configuration delegate to your UIAlertController

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter your phone number" message:nil preferredStyle:UIAlertControllerStyleAlert];


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{

textField.delegate = self;
}]; 

for more info you get the sample here

shim
  • 9,289
  • 12
  • 69
  • 108
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143