Best way to "clear all" values in text fields in all views with XIB based tab bar application

StackOverflow https://stackoverflow.com/questions/18626444

  •  27-06-2022
  •  | 
  •  

Question

I have been struggling with this for quite a while, and have quite a bit of research both here and elsewhere trying to figure out the best way forward.

I have a tab bar application with four tabs - three of the tabs receive user entry and compute values. Some of those values are forwarded to the other views (because they are used again in related calculations). That isn't my problem - I'm able to send the data forwards and backwards between the views, but what I'm having difficulty with is actually "clearing" all the values from all the views in response to a UILongPress gesture recognizer. I have a "Calculate" button on each view that computes the result, and a "Clear" button which clears the values on the displayed view.

This is Version 2 of the application, and I want to extend its functionality by allowing the user to hold the clear button down on any of the views and clear all of the values out.

Doesn't seem too difficult, but it certainly has been for me.

I've looked at using global variables, singletons, NSUserDefaults, NSNotification -- all with no luck. I haven't tried key value observing (KVO) yet, but I think a lot of my problem stems from the fact that I am using extremely old code -- my AppDelegate.m file has the older "DidFinishLaunching" method instead of the newer "ApplicationDidFinishLaunchingWithOptions" plus, the tab bar and view controllers were all created in Interface Builder, not programatically as they are in the newer XCode tab bar templates.

I suspect my problem might be in my use of viewWillAppear and/or viewDidLoad -- I use viewWillAppear in each of my viewcontroller.m files and that works quite well. At the core of this, I should be able to set a global BOOL somehow ("Clear all pressed?" YES or NO) and when the view appears, look at that variable, and if it's YES, set the respective uitextfield.text property to nil -- if it's NO, continue with the normal initialization and display.)

I tried awakeFromNib as well, no luck.

Before I post any code, anyone have an idea of what I might be able to try? Thank you kindly for any help that is provided!

Was it helpful?

Solution

Loop through all the viewcontrollers in your tabbarcontroller then loop through the subviews of your each view controller's view and if the view is a textfield, clear it.

- (void)clearAll
{
    for (UIViewController *vc in self.tabBarController.viewControllers) {
        for (UIView *view in vc.view.subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                UITextField *textField = (UITextField *)view;
                textField.text = nil;
            }
        }
    }
}

OTHER TIPS

Update for swift 3:

for view: UIView in self.view.subviews {
                if (view is UITextField) {
                    let textField: UITextField? = (view as? UITextField)
                    textField?.text = nil
                }
            }

Update: Swift 4

extension UIViewController{
    public func clearAllTexts(){
        for view in self.view.subviews{
            if view is UITextField{
                let field: UITextField = view as! UITextField
                field.text = ""
            }
        }
    }
}

Calling: self.clearAllTexts()

Try this simple solution.

for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        textField.text = @"";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top