简体   繁体   English

TextFieldShould中的UIAlertcontroller应该开始编辑Objective-C

[英]UIAlertcontroller in TextfieldShouldBeginediting objective-c

I want to show alert before text enter in textfield after ok button click textfield will be editable but I am showing alert but textfield did not editable. 在单击确定按钮后,我想在文本输入文本字段之前显示警报,但文本字段是可编辑的,但我显示警报,但文本字段不可编辑。

if(textField == contractValueField)
{
    [self showAlertWithTitle:@"Information" textfield:contractValueField];
    return YES;
}

I search so many sites but I did not get right answer by using UIAlertView it is working but I want to use UIAlertController. 我搜索了很多站点,但是使用UIAlertView却没有得到正确的答案,但是我想使用UIAlertController。 I have more than 20 textfields in my project & every textfield I will show alert before edit begin. 我的项目中有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];
}

The reason is that when you call [textfield becomeFirstResponder] , -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField is also called again, which in turn shows your alertView, and when the ok button is selected from your alert, the alert view is shown again, and you are stuck in a loop. 原因是,当您调用[textfield becomeFirstResponder] ,还会再次调用-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField ,这反过来又显示您的alertView,并且当从警报中选择“确定”按钮时,警报视图为再次显示,您就陷入了循环。 Here is a quick way to fix it: 这是修复它的快速方法:

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

The code for your alertView should be as follows: 您的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 the tag value of textField back to 0 when editing is finished: 编辑完成后,将textField的标记值恢复为0:

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

Declare a UITextField which will reference the last selected textField: 声明一个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];
}

This would be the better option, I didn't realise that you wanted to show the alert every even if the textField had been selected previously 这将是更好的选择,即使您之前选择了textField,我也没有意识到您想显示所有警报

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

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