禁用按钮,直到输入文本字段? [英] Disable button until text fields have been entered?

查看:156
本文介绍了禁用按钮,直到输入文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中有几个uitextfields,我想禁用uibutton,直到所有字段都输入了一些内容。这样做的最佳方法是什么?
理想情况下,我也想做一些基本的验证(确保所有条目都是数字)。

I have several uitextfields within a view and I would like to disable the uibutton until all the fields have something entered into them. What's the best way of doing this? Ideally, I'd like to do some basic validation (make sure all entries are numbers) too.

编辑

无法获得以下解决方案。下面是我工作的版本(从Brad,Mike和其他各种来源拼凑而成)

Couldn't get the solutions below to quite work. Below is the version that I got working (cobbled together from Brad, Mike, and various other sources)

使用UITextFieldDelegate

Use the UITextFieldDelegate

在IB中创建文本字段,并附加到相关的IBOutlets - textField1,textField2等

Create textfields in IB, and attach to the relevant IBOutlets - textField1, textField2 etc

创建并将按钮连接到其相关的IBOutlet(submitButton)和IBAction( IB中的submitAction)。取消选中在IB中启用。

Create and hookup the button to its relevant IBOutlet (submitButton) and IBAction (submitAction) in IB. Uncheck enabled in IB.

在视图控制器中创建验证方法:

Create a validate method in the view controller:

-(IBAction)validateTextFields:(id)sender
    {
        // make sure all fields are have something in them
        if ((textField1.text.length  > 0) && (textField2.text.length > 0)  && (textField3.text.length > 0)) {
            self.submitButton.enabled = YES;
        }
        else {
            self.submitButton.enabled = NO;
        }
    }

将每个字段'Editing Changed'事件连接到validateTextFields方法。 (注意:'Value Changed'事件似乎不起作用)

Hookup each fields 'Editing Changed' event to the validateTextFields method. (Note: The 'Value Changed' event doesn't appear to work)

使用此委托方法限制字符(在我的情况下为数字,最多为一个-停)。这个位来自Erica Sadun btw。

Use this delegate method to limit characters (in my case to numbers, and up to one full-stop). This bit is from Erica Sadun btw.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    NSString *filtered;
    // Check for period
    if ([textField.text rangeOfString:@"."].location == NSNotFound)
    {
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
        filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    // Period is in use
    cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

Voila。

推荐答案

您需要在管理视图的视图cOntroller中的所有文本字段上实现UITextFieldDelegate委托,确保在加载视图后验证文本字段:

You will want to implement the UITextFieldDelegate Delegate on all of your textfields in a view cOntroller managing your view like so, making sure to validate your textfields after loading your view:

-(void) viewDidLoad {
  textField1.delegate = self;  //Note if these were created in a xib, you can do this in IB
  textField2.delegate = self;
  [self validateTextFields];
}

并实施textField:shouldChangeCharatersInRage:方法,以便每次textfileds更改时进行验证:

And Implement the textField:shouldChangeCharatersInRage: method to due the validation everytime the textfileds change:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  [self validateTextFields];
  return YES;
}

最后,根据需要进行实际验证并启用或禁用按钮:

Finally, do the actual validation and enable or disable your button as needed:

-(void) validateTextFields {
  if ((textField1.text.length > 0) && textField2.text.length > 0)) {
    myUIButton.enabled = YES;
  }
  else {
    myUIButton.enabled = NO;
  }
}

这篇关于禁用按钮,直到输入文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆