我在我的应用程序上发现了一个非常奇怪的行为, View Controller 的结构如下:
TabBarViewController
包含:
- NavigationController(root is HomeViewController)
- NavigationController(root is SearchViewController)
- NavigationController(root is ProfileViewController)
当我在 HomeViewController 中时,我以模态方式呈现 PremiumViewController
@objc func premiumTapped() {
let premiumViewController = PremiumViewController(viewModel: PremiumViewModel(networkService: NetworkService(), purchasesService: PurchasesService.shared))
navigationController?.present(premiumViewController, animated: true, completion: nil)
}
到这里为止一切都很好。
现在我解雇了PremiumViewController。
dismiss(animated: true, completion: nil)
发生的情况是 HomeViewController
调用它的 init()
和 viewDidLoad()
!
这怎么可能? HomeViewController
一直存在,为什么又调用 init 呢?
最佳答案
感谢@rmaddy,我发现了这个问题。
为了找到答案,我在一直调用的 init()
函数中放置了一个断点。我发现调用它的是来自 TabbarViewController
的 viewWillAppear()
。
问题是我在 viewWillAppear()
上初始化选项卡栏 View Controller ,而不是在 viewDidLoad()
上,这使得每次重新创建所有选项卡 Controller View 出现。
关于ios - 呈现一个新的 View Controller ,然后关闭它,在父 View Controller 上调用 init(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57791163/