【问题标题】:how to disable the both uialertcontroller button when no input in uitext field of uialertview controller [duplicate]当 uialertviewcontroller 的 ui 文本字段中没有输入时如何禁用两个 uialertcontroller 按钮 [重复]
【发布时间】:2016-08-12 03:58:51
【问题描述】:

看看我下面的代码。我正在尝试使用 uialertviewcontroller 添加文本字段。现在,当我的两个文本字段中没有输入时,我需要禁用两个操作按钮。如何做到这一点??

请提出一些可能的解决方案。谢谢!

【问题讨论】:

标签: ios objective-c uialertcontroller


【解决方案1】:

最初禁用按钮,然后在文本字段中有内容后重新启用它们。

UIAlertController *alert = [UIAlertController
                                  alertControllerWithTitle:@"Info"
                                  message:@"You are using UIAlertController with Actionsheet and text fields"


preferredStyle:UIAlertControllerStyleAlert];


UIAlertAction* ok = [UIAlertAction
                     actionWithTitle:@"OK"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction * action)
                     {
                         NSLog(@"Resolving UIAlert Action for tapping OK Button");
                         [alert dismissViewControllerAnimated:YES completion:nil];

                     }];
UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             NSLog(@"Resolving UIAlertActionController for tapping cancel button");
                             [alert dismissViewControllerAnimated:YES completion:nil];

                         }];
[ok setEnabled:false];

[cancel setEnabled:false];

[alert addAction:ok];
[alert addAction:cancel];

将选择器添加到文本字段然后处理它:

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"iCloud ID";
    textField.textColor = [UIColor blueColor];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [textField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
}];

- (void)textDidChange:(UITextField *)textField {
    if (textField.text.length > 0) {
        // enable the buttons
    }
}

要处理文本字段代表,您可以阅读以下帖子:

【讨论】:

  • 我将为您提供解决方案。如果我的两个文本字段为空会发生什么情况。然后我将如何将我的两个按钮设置为禁用。如果我的两个文本字段都已填充,那么我如何启用我的两个按钮
  • 根据我上面的问题代码。如何在 if 语句中调用我的文本字段来设置启用我的按钮
  • 为您的两个操作创建两个类级别的变量。最初将这些操作设置为禁用,就像我在上面的示例中所做的那样。现在将您在操作中添加的文本字段的委托设置为 self 并实现UITextFieldDelegate。一旦您的两个文本字段都有内容,请使用您创建的类变量启用操作。
  • 如果 uitext 字段被填充,您能否提供一些 uitextfield 委托的示例代码以启用我的按钮
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
相关资源
最近更新 更多