Objective c UIAlertController:textfield未返回字符串
有些意想不到的事情我无法解决。我有一个带有Objective c UIAlertController:textfield未返回字符串,objective-c,string,uitextfield,uialertcontroller,Objective C,String,Uitextfield,Uialertcontroller,有些意想不到的事情我无法解决。我有一个带有textfields的alertController。我尝试获取其中一个的字符串值。当字符串长度小于11个字符时,一切正常。高于此阈值时,字符串为null 谁能告诉我发生了什么事 为了以防万一,我将代码放在下面: [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"Name";
textfields
的alertController
。我尝试获取其中一个的字符串值。当字符串长度小于11个字符时,一切正常。高于此阈值时,字符串为null
谁能告诉我发生了什么事
为了以防万一,我将代码放在下面:
[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
}]];
谢谢 是否确定^(UITextField*textField)
和nameTextfield
都相等,请确保获取当前的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
}]];
我更改了你的代码,运行它,确保彼此的地址相同
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
包含更多文本字段。因此,您可以使用标记来标识文本字段。是,它是相同的地址。它看起来很好,因为当我的字符串少于12个字符时没有问题。只有当字符串长度超过此限制时,我才会得到错误。还有其他建议吗?我注意到textfield文本(nameTextfield.text)即使使用更长的字符串也可以,但只有我的字符串属性(self.textFieldString)在尝试获取大于11个字符的字符串时保持空值,好像不可能将一个长度超过这个长度的字符串归为一个属性。你有你的解决方案吗?还没有,但我还没有时间去挖掘它(病态…)。我会让你知道的。谢谢你的邀请!显然,这个问题与“弱”属性赋值有关,但我无法理解为什么它使用较短的字符串长度。一个谜。但是谢谢你的帮助!