0

My app has a ViewController consisting multiple textfield and custom UI elements arranged as a form with "submit" and "reset" buttons.

I want to reset all the textfields and custom UI elements when user clicks the submit button so that user gets a feeling that same Form is opened again.

I tried calling ViewDidLoad() and setNeedsDisplay() on the click of "Submit" button but data previously filled by user remains as it is.

Kindly Help!

ujjwal
  • 428
  • 3
  • 16

2 Answers2

1

Just set the text of all textfields to an empty string (the text value). There is no magic function to clean everything in a view.

The only other solution would be to actually display the ViewController again, but this is probably not what you want, because it causes overhead as well as you might see something of the switching on the screen.

Nef10
  • 926
  • 2
  • 16
  • 26
  • I would prefer to display ViewController again rather clearing all text fields manually. But the problem is that, I am using navigation controller so If I will push same ViewController again then when user will click back, he will see the previous filled view. How can I pop current view controller and push same view controller again? – ujjwal May 02 '17 at 20:19
  • You can either pop the old ViewController from the navigation stack before presenting another instance of it again (using `popViewController(animated:)`) or you change the behaviour of the back button as discussed [here](http://stackoverflow.com/questions/18824186/trying-to-handle-back-navigation-button-action-in-ios) – Nef10 May 02 '17 at 20:24
  • 3
    I'd advise to just clear the text fields manually. There are IBOutletCollections, if you used the IB, so you can just iterate through the collection easily with a function. Alternatively, you could iterate through the UIViewController's subviews, check if the class is of type UITextField, and if so, reset it. – Jake T. May 02 '17 at 20:36
-1

If you programmatically coded your textfields and some other objects, you can refresh always your viewController everytime by adding all your UI codes in the predefined method: -(void)viewWillAppear:(BOOL)animated{} and you should remove all self.view elements by adding this code. [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

You codes in the viewWillAppear will look like this.

-(void)viewWillAppear:(BOOL)animated{
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

// Codes for User Interface here 

}
handiansom
  • 783
  • 11
  • 27