简体   繁体   English

检查UITextFields是否有文字

[英]Check UITextFields for text

There is a view controller on my app in which the user enters personal information. 我的应用程序上有一个视图控制器,用户可以在其中输入个人信息。 There is a cancel option that pulls up an alert notifying them they will lose the data if it is not saved. 有一个取消选项,它会弹出一个警报,通知他们如果不保存数据,它们将丢失数据。 I only want to display this alert if any text field in this view controller has [.text.length > 0] (I have about 20 text fields, if any have even 1 character it should pull up the alert). 我只想在此视图控制器中的任何文本字段具有[.text.length> 0]的情况下显示此警报(我有大约20个文本字段,如果有甚至1个字符,它都应拉出警报)。 I could manually name every text field in an if statement but was hoping there was some way to check all the text fields in my view controller? 我可以在if语句中手动命名每个文本字段,但希望有某种方法可以检查视图控制器中的所有文本字段?

Here's what I have so far: 这是我到目前为止的内容:

for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        if(textField.text.length >= 1){
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cancel?"
                                                                message:@"If you leave before saving, the athlete will be lost. Are you sure you want to cancel?"
                                                               delegate:self
                                                      cancelButtonTitle:@"No"
                                                      otherButtonTitles:@"Yes", nil];

            [alertView show];
        }
        if(textField.text.length == 0){
            [[self navigationController] popViewControllerAnimated:YES];
        }
    }
}

I want to check if there are any text fields with value but this causes errors because its checking if textField.text.length == 0 before it finishes the for loop. 我想检查是否有带值的文本字段,但这会导致错误,因为它在完成for循环之前检查textField.text.length == 0。

SOLUTION: 解:

BOOL areAnyTextFieldsFilled = NO;
for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        if(textField.text.length >= 1){
            areAnyTextFieldsFilled = YES;

        }

    }
}
if(areAnyTextFieldsFilled == YES){
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cancel?"
                                                        message:@"If you leave before saving, the athlete will be lost. Are you sure you want to cancel?"
                                                       delegate:self
                                              cancelButtonTitle:@"No"
                                              otherButtonTitles:@"Yes", nil];

    [alertView show];
}
else{
    [[self navigationController] popViewControllerAnimated:YES];
}

You can achieve something very nice with tags. 您可以使用标签实现一些非常好的功能。 Set each one to a certain tag 1, 2, ... , 20. Then, iterate through with something like this: 将每个标签设置为一个特定的标签1、2,...,20。然后,使用以下内容进行迭代:

    for (int x = 1; x < 21; x++)
    {
        UITextField *aTextField = (UITextField *) [self.view viewWithTag:x];
        //check here if text
    }

Also, for additional efficiency, you could set a flag that starts as zero and if a user edits a textField, it will change to one. 另外,为了提高效率,您可以设置一个从零开始的标志,并且如果用户编辑textField,它将变为1。 Then it would only loop if it is one. 然后它只会循环如果它是一个。


However, for your current method what you could do is have a flag (yes, again, I love them :) ) And set it to zero before hand (probably in viewDidLoad). 但是,对于您当前的方法,您可以做的是拥有一个标志(是的,再次,我爱他们:)),然后将其设置为零(可能在viewDidLoad中)。 If you run into a text field with words, set it to 1. Erase the current if(textField.text.length == 0) statement. 如果遇到带有单词的文本字段,请将其设置为1。删除当前的if(textField.text.length == 0)语句。 Then, afterwards just check if the flag still == 0 after the loop. 然后,之后只需检查循环后标志是否仍== 0。

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

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