0

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()   
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
adrapp
  • 47
  • 6

3 Answers3

0

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
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

You could use a delegate method to perform some changes in VC1 in response to some action in VC2. In this case you will set the delegate in VC1 and call the delegate method in VC2. Ideal place to make this call would be in completion block of dismiss.

//VC1
public protocol MyProtocol: class {
    func delegateMethod()
}

In the viewDidLoad method set the delegate for VC2

override func viewDidLoad() {
    super.viewDidLoad()
    fetchUser()
    setupUI()
    //VC2 is the instance of view controller you are going to push from this page
    VC2.delegate = self
}

Make sure VC1 confirms to MyProtocol protocol

extension VC1: MyProtocol {
    func delegateMethod() {
        // reload view here
    }
}

Declare the delegate in VC2

//VC2
var delegate: MyProtocol?    

Then call the delegate method in completion of dismiss

self.dismiss(animated: false, completion: {
    self.delegate?.delegateMethod()
})

Alternatively you could use observers to respond to any changes as well, but that might be an overkill. Check out this article, they discuss the whole thing in detail.

Deeksha Kaul
  • 264
  • 2
  • 7
  • Does this also apply if I'm using a tableview (bottom elements of the UI) that needs to be updated vice labels (top elements)? – adrapp Jun 30 '20 at 23:58
  • It should still work. Delegate methods are used to pass data between view controllers. Once the data you need is in VC1, you should be able to update tableView, labels anything. It shouldn't make a difference. Same goes with observers, if you have the updated value you should be able to update/reload any part of the view. – Deeksha Kaul Jul 02 '20 at 00:15
  • Thank you @Deeksha – adrapp Jul 02 '20 at 15:57
  • So I tried your method, the UIElements in the top (image, labels) refresh correctly, but the tableview in the bottom doesn't. When I close the app and reopen it, the tableview is refreshed correctly. Any way you can point me in the right direction? Much appreciated. – adrapp Jul 02 '20 at 18:34
  • Try calling tableview.reloadData() when you refresh the labels – Deeksha Kaul Jul 08 '20 at 00:15
0

setting vc2.modalPresentationStyle = .fullScreen will solve it without the need to make any delegates.