我想在确定按钮单击文本字段后在文本字段中输入文本之前显示警报文本字段将可编辑但我显示警报但文本字段不可编辑。
if(textField == contractValueField)
{
[self showAlertWithTitle:@"Information" textfield:contractValueField];
return YES;
}
我搜索了很多网站,但使用 UIAlertView 没有得到正确的答案,但我想使用 UIAlertController。我的项目中有 20 多个文本字段,每个文本字段在编辑开始之前都会显示警报。
- (void)showAlertWithTitle:(NSString *)title textfield:(UITextField*)textfield
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"Please enter Amount in Lakhs" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
[textfield becomeFirstResponder];
}];
[alert addAction:ok];
[self.navigationController presentViewController:alert animated:YES completion:nil];
}
原因是当你调用时[textfield becomeFirstResponder]
,-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
也被再次调用,反过来显示你的alertView,当你的alert中选择ok按钮时,又会显示alert视图,你就卡在了一个循环中。这是修复它的快速方法:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// check to see if the alert has already been shown, if it has the tag value will be 1
if (textField == contractValueField && textField.tag != 1) {
[self showAlertWithTitle:@"Information" textfield:textField];
return false;
}
return true;
}
alertView 的代码应如下所示:
- (void)showAlertWithTitle:(NSString *)title textfield:(UITextField*)textfield{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"Please enter Amount in Lakhs" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
textfield.tag = 1;
[alert dismissViewControllerAnimated:YES completion:nil];
[textfield becomeFirstResponder];
}];
[alert addAction:ok];
[self presentViewController:alert animated:true completion:nil];
}
编辑完成后,将 textField 的标签值恢复为 0:
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
textField.tag = 0;
return true;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句