0

I have a customerViewController that has a simple a form. When the user presses submit, a segue is triggered and another view appears. The problem occurs when the user goes back to the customerViewController and finds all of the old information still there. I could simply reset the form fields, but what I'd really like is to find a way to reset the entire VC. From what I've learned so far, the way to reset a vc that hasn't been pushed is to remove it and then add it back.

customerViewController is the initial view controller in a navigation controller which is embedded in a tab bar controller. I have a tabBarController class that is a UITabBarControllerDelegate. This is where I call:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {  
   if item.tag == 2 {   //This is the tab with my navigation controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "CustomerVCID")
        var viewcontrollers = self.navigationController?.viewControllers

        viewcontrollers?.removeFirst()
        viewControllers?.insert(vc, at: 0)
        self.navigationController?.setViewControllers(viewcontrollers!, animated: true) 
}

The problem with my code is that navigationController?.viewControllers is nil in the code above. I can reference viewControllers which gives me a list of tab bar viewControllers, but I'm not sure how to get from there to the navigation controller.

I guess my question is, assuming that I'm on the right track, how do I reference the view controllers in my navigation controller?

squarehippo10
  • 1,855
  • 1
  • 15
  • 45
  • As i read your comment in the answer below. You can always reach into the navigationControllers viewController stack and remove / append a new one – Vollan May 03 '19 at 09:33
  • That sounds like exactly what I'd like to do. I just have to figure out how to get past the tab bar controller and into the navigation controller. – squarehippo10 May 03 '19 at 17:12

2 Answers2

0

You can reset your form values in vc inside the viewWillAppear(_:),

class ViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
       //clear the textfields, textviews values etc. here. 
    }
}
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • That's true, I could just clear each of the form fields. But they all have titles with animated constraints and that would take a little work to reset properly. Which is why I'm hoping there's a way to get a new instance of that controller with each visit. – squarehippo10 May 03 '19 at 06:48
0

It turns out, I was over-complicating things by trying to access navigationController.viewControllers or tabBarController.viewControllers. All I needed was viewControllers which is a property of UITabBarController that contains an array of the controllers associated with each tab:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if item.tag == 2 {  //tab with navigation controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vcon = storyboard.instantiateViewController(withIdentifier: "CustomerVCID")

        for viewcontroller in viewControllers! {
            if let vc = viewcontroller as? UINavigationController {
                vc.viewControllers.removeFirst()
                vc.viewControllers.insert(vcon, at: 0)
                vc.setViewControllers(vc.viewControllers, animated: true)
            }
        }
    }
squarehippo10
  • 1,855
  • 1
  • 15
  • 45