Search code examples
iosswiftuitabbarcontrolleruitabbaritem

View Controller1 in a TabBarController won't automatically update view after ViewController2 is dismissed


enter image description here

SCENARIO

Xcode 11.5, Swift 5

  1. Using Core Data
  2. User wants to update their profile. VC2 is dismissed after user taps save. VC1 area highlighted in yellow should reflect the change.

PROBLEM

Data is being saved correctly. However, VC1 elements highlighted in yellow doesn't automatically update. If I go to another tab then come back, the view elements refresh with the updated changes.

MY CODE

I have a setupUI() method that lays out the elements and have tried adding it to VC1's viewWillAppear method, but no luck.

   //VC1:

   override func viewDidLoad() {
    super.viewDidLoad()
    fetchUser()
    setupUI()
    
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    
    setupUI()   
}

Solution

  • viewWillAppear is not called when you dimiss the modal that fill the data you need to use a delegate

    1- When you show the modal vc

    let vc = SomeVC()
    vc.delegate = self // declare property delegate inside the modal of that type / protocol 
    // present
    

    2- when you dimiss the modal

    self.delegate?.setupUI()
    // dimiss