UIAlertController addTextFieldWithConfigurationHandler 使用全局变量

UIAlertController addTextFieldWithConfigurationHandler using global variable

我正在调用带有 2 个文本字段的 UIAlertController(在 Obj-C 中)。我已经定义了我需要在警报中使用的全局 UITextFields,因为我需要利用 UITextFieldDelegate。

我的文本字段是在 .h 文件中定义的。

@property (nonatomic, strong) UITextField *nameField;
@property (nonatomic, strong) UITextField *phoneField;

有没有办法从这里重铸 addTextFieldWithConfigurationHandler 块:

[alert addTextFieldWithConfigurationHandler:^(UITextField *myField) {
        myField.delegate = self;
    }];

像这样...我可以在哪里分配我之前定义的文本字段以在这个块中使用?

[alert addTextFieldWithConfigurationHandler:^(**nameField**) {
            nameField.delegate = self;
        }];

我想要做的是:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == nameField) {
        phoneField.text = @"";
    }

    if (textField == phoneField) {
        nameField.text = @"";
    }
}

如果一个字段有文本,则另一个字段应为空白。它在 alertview 之外工作没有任何问题......但在块内,它不会清除其他字段。

如有任何帮助,我们将不胜感激。

将您的全局变量分配为块内的指针

[alert addTextFieldWithConfigurationHandler:^(UITextField *aTextField) {
        GLOBAL_VARIABLE = aTextField;//<- pointer
        aTextField.delegate = self;
    }];

然后你就可以在回调中正确使用你的条件语句了。

注意:将 GLOBAL_VARIABLE 替换为您的全局变量 textifled