shouldChangeCharactersInRange没有被调用

时间:2018-04-06 03:46:22

标签: ios objective-c uitextfield uialertcontroller

我在alertview中有一个名为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委托方法未被调用

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

}

1 个答案:

答案 0 :(得分:1)

phoneNumberTF不是UIalertController的一部分,因此不需要代理phoneNumberTF.delegate = self;

将configurationdelegate添加到您的UIAlertControler

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


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{

textField.delegate = self;
}]; 

有关详细信息,请获取示例here