How to reload a view controller when back from another view using tab bar

Bas*_*ien 2 uitabbarcontroller uiviewcontroller loadview ios swift

I have a common view used when the user opens the app (VC1). With a tab bar controller, I load another view (VC2) that can be used to update data visible in the previous one. When I go back on VC1 (which is stack), it does not reload with updated data.

I have tried to call the viewDidLoad in the viewWillAppear like so...

override func viewWillAppear(_ animated: Bool) {
    viewDidLoad()
}
Run Code Online (Sandbox Code Playgroud)

It works, but it loads over the VC1 still on stack and the user can see the change (not good).

I suppose that to dismiss the VC1 would help, but I haven't found how to dismiss a view when using tab bar controller.

Kru*_*nal 6

请按照以下步骤操作,以在每次视图出现时查看视图负载设置:
(VC1 = First View Controller)

  • viewLoadSetup在VC1中创建一个新的函数/方法(名为),然后将所有代码从viewDidLoad()移至viewLoadSetup()
  • 现在,呼叫viewLoadSetup()viewWillAppear

    class VC1: UIViewController {
    
        override func viewDidLoad() {
           super.viewDidLoad()
           // viewLoadSetup()  you may call it from view did load also
        }
    
        override func viewWillAppear(_ animated: Bool) {
           super.viewWillAppear(animated)
           viewLoadSetup()
    
        }
    
    
         func viewLoadSetup(){
          // setup view did load here
         }
    
    
    }
    
    Run Code Online (Sandbox Code Playgroud)


如果您想viewLoadSetupviewDidLoad进行呼叫,则是在加载视图控制器时调用一次,viewWillAppear然后从那时开始,

class VC1: UIViewController {

    var isLoadingViewController = false

    override func viewDidLoad() {
        super.viewDidLoad()
        isLoadingViewController = true
        viewLoadSetup()
    }

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

        if isLoadingViewController {
            isLoadingViewController = false
        } else {
            viewLoadSetup()
         }
    }


    func viewLoadSetup(){
      // setup view did load here
    }


}
Run Code Online (Sandbox Code Playgroud)