UIAlertController:文本字段不返回字符串

问题描述:

有些意外我无法解决.我有一个带有 textfieldsalertController.我尝试获取其中之一的字符串值.当字符串长度小于 11 个字符时,一切正常.高于此阈值,字符串为 null.

There is something unexpected I cannot sort out. I have an alertController with textfields. I try to get the string value of one of them. Everything works fine when the string length is less than 11 characters. Above this threshold, the string is null.

有人能告诉我发生了什么吗?

Could anyone give me a hint on what there is going on?

为了以防万一,我把我的代码放在下面:

Just in case, I put my code below:

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Name";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];

[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray *textfields = alertController.textFields;
        UITextField *nameTextfield = textfields[0];
        self.textFieldString = nameTextfield.text;

        NSLog(@"self.textFieldString is: %@", self.textFieldString); // -> this returns a null value when the string length is > 11


    }]];

谢谢!

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Name";
    textField.textColor = [UIColor blueColor];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
}];

[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    NSArray *textfields = alertController.textFields;
    UITextField *nameTextfield = textfields[0];
    //        self.textFieldString = ;

    NSLog(@"self.textFieldString is: %@", nameTextfield.text); // -> this returns a null value when the string length is > 11


}]];
[self presentViewController:alertController animated:YES completion:nil];

注意:- 如果您添加更多文本字段,则 NSArray *textfields 包含更多文本字段.所以你可以使用标签来识别文本字段.

Note :- If You add more textfields then NSArray *textfields contains more textfields. So you can use tag for identify textfields.