【问题标题】:UIAlertController: textfield not returning stringUIAlertController:文本字段不返回字符串
【发布时间】:2017-01-06 21:31:48
【问题描述】:

有一些意想不到的事情我无法解决。我有一个alertControllertextfields。我尝试获取其中一个的字符串值。当字符串长度小于 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


    }]];

谢谢!

【问题讨论】:

  • 你有解决方案了吗?
  • 还没有,但我还没有时间去挖掘它(生病......)。我会让你知道。谢谢提问!
  • 显然问题与“弱”属性分配有关,但我不明白为什么它使用较短的字符串长度。一个谜。但是感谢您的帮助!

标签: objective-c string uitextfield uialertcontroller


【解决方案1】:

您确定^(UITextField *textField)nameTextfield 等于两者吗,请确保您获得当前文本字段。

[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


}]];

我更改了你的代码,运行它,确保地址彼此相同。

【讨论】:

  • 是的,地址相同。它看起来很好,因为当我的字符串少于 12 个字符时没有问题。当字符串长度超出此限制时,我只会收到错误消息。还有其他建议吗?
  • 我注意到文本字段文本 (nameTextfield.text) 即使使用更长的字符串也可以,但只有我的字符串属性 (self.textFieldString) 在尝试获取 > 11 个字符的字符串时保持为空,如如果无法将长度超过此长度的字符串归因于
【解决方案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];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多