TextfieldShouldBeginediting objective-c中的UIAlertcontroller

时间:2017-12-06 07:16:17

标签: ios objective-c uitextfield uialertcontroller

我希望在文本输入文本字段之前显示警告,然后确定按钮单击文本字段可以编辑,但我显示警报但文本字段无法编辑。

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

2 个答案:

答案 0 :(得分:2)

原因是当您拨打[textfield becomeFirstResponder]时,还会再次调用-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField,然后再显示您的alertView,当您从警报中选择确定按钮时,会再次显示警报视图,你陷入了困境。这是一个快速解决方法:

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

答案 1 :(得分:0)

声明一个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