我正在调用带有2个文本字段的UIAlertController(在Obj-C中)。我已经定义了需要在警报中使用的全局UITextField,因为我需要利用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
替换为您的全局变量文本文件
关于ios - UIAlertController addTextFieldWithConfigurationHandler使用全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42238940/