【问题标题】:UIAlertcontroller in TextfieldShouldBeginediting objective-cTextfieldShouldBeginediting Objective-c 中的 UIAlertcontroller
【发布时间】:2017-12-06 07:16:17
【问题描述】:

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

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

【问题讨论】:

  • 你的 showAlertWithTitle: 方法被调用了吗?
  • 是的,它正在调用,但按确定后文本字段不可编辑
  • 试试 [self presentViewController],我认为它不起作用
  • 不,它不工作
  • 尝试在dismissViewController的完成块中加入becomeFirstResponder

标签: ios objective-c uitextfield uialertcontroller


【解决方案1】:

原因是当你调用[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];
}

编辑完成后将textField的标签值恢复为0:

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

【讨论】:

  • 第一次显示警报时,我也会编辑文本,但我单击另一个文本字段然后返回并单击此文本字段,它没有显示警报。我想在每次点击文本字段时显示警报将显示然后编辑。
  • 非常感谢它正在工作。有没有其他方法可以不用标签,比如 - textfield.selected=yes;
  • 我还有一个想法,我会添加它作为另一个答案
【解决方案2】:

声明一个 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,您也希望每次都显示警报

【讨论】:

  • 但我将无法使用此代码,因为我有一些文本字段将返回 false;
  • 我认为您应该能够在textFieldShouldBeginEditing 中添加另一个else if() 语句来解决这个问题,但是如果不了解更多关于您的项目及其功能的信息,就很难再提供任何帮助。 ..
  • 非常感谢您的快速回答。我尝试了另一个 if else () 它正在工作。
  • 完全不用担心
猜你喜欢
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多