简体   繁体   中英

Presenting a new view controller and then dismissing it, calls init() on the parent view controller

I am finding a very weird behaviour on my app, the structure of the view controllers is the following:

TabBarViewController contains:

- NavigationController(root is HomeViewController)
- NavigationController(root is SearchViewController)
- NavigationController(root is ProfileViewController)

When I am in HomeViewController, I present modally PremiumViewController

@objc func premiumTapped() {
    let premiumViewController = PremiumViewController(viewModel: PremiumViewModel(networkService: NetworkService(), purchasesService: PurchasesService.shared))
    navigationController?.present(premiumViewController, animated: true, completion: nil)
}

All fine until here.

Now I dismiss PremiumViewController.

dismiss(animated: true, completion: nil)

What happens is that HomeViewController calles it's init() and viewDidLoad() !

How is this possible? HomeViewController has been existing all this time, so why it calls init again?

Thanks to @rmaddy I found the issue.

To find it out, I put a breakpoint in the init() function that was called all the time. I found out that who was calling it was viewWillAppear() from the TabbarViewController .

The issue was that I initialise the tabbar view controller on the viewWillAppear() rather than on viewDidLoad() , which made recreate all the tab controllers every time one of the views appeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM