问题
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
}]];
Thanks!
回答1:
are you sure ^(UITextField *textField)
and nameTextfield
is equal to both, make sure you get the current textfield.
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
**NSLog(@"%s", 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];
**NSLog(@"%s", nameTextfield);**
self.textFieldString = nameTextfield.text;
NSLog(@"self.textFieldString is: %@", self.textFieldString); // -> this returns a null value when the string length is > 11
}]];
i changed your code , run it ,make sure the address same to each other.
回答2:
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];
Note :- If You add more textfields then NSArray *textfields
contains more textfields. So you can use tag for identify textfields.
来源:https://stackoverflow.com/questions/41514696/uialertcontroller-textfield-not-returning-string