首页 > 解决方案 > UIAlertController 文本中的 UITextField 为 NULL 并且不显示任何文本

问题描述

我已经提供了一个带有文本字段的 UIAlertController,但是每当我在文本字段上输入任何内容时都会收到警告,例如请求 caretRectForPosition:而 NSTextStorage 有未完成的更改 {0, 0}。我的代码是

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


[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.keyboardType = UIKeyboardTypeNumberPad;
   // textField.secureTextEntry = YES;
    //textField.delegate = self;
}];

UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    NSArray * textfields = alertController.textFields;
    UITextField * cardField = textfields[0];
    NSLog(@"Current password %@", [cardField text]);

    if ([cardField hasText]) {
        //will do something
    }
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"Canelled");
}];

[alertController addAction:confirmAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

错误是requesting caretRectForPosition: while the NSTextStorage has oustanding changes {0, 0}

标签: iosobjective-cuialertcontroller

解决方案


推荐阅读