TextfieldShouldBeginediting 中的 UIAlertcontroller objective-c

UIAlertcontroller in TextfieldShouldBeginediting objective-c

我想在点击确定按钮后在文本字段中输入文本之前显示警报文本字段将是可编辑的,但我显示警报但文本字段不可编辑。

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 view是再次显示,你陷入了一个循环。这是修复它的快速方法:

-(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];
}

Return编辑完成后textField的tag值归0:

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    textField.tag = 0;
    return true;
}

声明一个 UITextField,它将引用最后选择的 textField:

@interface ViewController (){
    UITextField *lastSelectedTextField;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    // check to see if the textField is the last shown textField
    if (textField == lastSelectedTextField) {
        return true;
    }
    [self showAlertWithTitle:@"Information" textfield:textField];
    return false;
 }


- (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){
        lastSelectedTextField = textfield;;
        [alert dismissViewControllerAnimated:YES completion:nil];
        [textfield becomeFirstResponder];
    }];

    [alert addAction:ok];
    [self presentViewController:alert animated:true completion:nil];
}

这将是更好的选择,我没有意识到即使之前已经选择了 textField,您也想每隔一段时间显示一次警报