繁体   English   中英

TextFieldShould中的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,并且当从警报中选择“确定”按钮时,警报视图为再次显示,您就陷入了循环。 这是修复它的快速方法:

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

声明一个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,我也没有意识到您想显示所有警报

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM