简体   繁体   中英

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). 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?

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.

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:

    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. 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). If you run into a text field with words, set it to 1. Erase the current if(textField.text.length == 0) statement. Then, afterwards just check if the flag still == 0 after the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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