[英]viewWillAppear not called apparently due to my view hierarchy
This appears to be a well documented problem, yet the solutions online have not worked.这似乎是一个有据可查的问题,但在线解决方案没有奏效。 Here's just a sample list of posts that failed to provide me with a working answer:这只是未能为我提供有效答案的帖子示例列表:
I have gleaned that my problem with viewWillAppear not getting called has to do with my view hierarchy.我发现我的 viewWillAppear 没有被调用的问题与我的视图层次结构有关。 I am using a tab controller that is not the highest part of the view hierarchy.我使用的选项卡控制器不是视图层次结构的最高部分。 One of the tab controller's view controllers is a root view controller to a navigation controller.选项卡控制器的视图控制器之一是导航控制器的根视图控制器。 That's where I am trying to get a working viewWillAppear or viewDidAppear.这就是我试图获得有效 viewWillAppear 或 viewDidAppear 的地方。 Here's what I tried that has not worked.这是我尝试过但没有奏效的方法。 Within the tab controller I added this code:在选项卡控制器中,我添加了以下代码:
let nav2 = UINavigationController(rootViewController: locationsVC)
nav2.beginAppearanceTransition(true, animated: false)
//...//
viewControllers = [ nav1, nav2, nav3, nav4 ]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for vc in self.children {
vc.beginAppearanceTransition(true, animated: animated)
}
}
In the scene delegate, this is my code:在场景委托中,这是我的代码:
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
let rootVC = NewOrExistingViewController()
rootVC.beginAppearanceTransition(true, animated: false)
let rootNC = UINavigationController(rootViewController: rootVC)
rootNC.navigationController?.navigationBar.isHidden = true
rootNC.beginAppearanceTransition(true, animated: false)
self.window?.rootViewController = rootNC
let tbc = TabBarViewController()
tbc.selectedIndex = 0
tbc.beginAppearanceTransition(true, animated: false)
rootVC.add(asChildViewController: tbc)
Try calling rootVC.add(asChildViewController: tbc)
before rootVC.beginAppearanceTransition(true, animated: false)
.尝试在rootVC.beginAppearanceTransition(true, animated: false)
之前调用rootVC.add(asChildViewController: tbc)
rootVC.beginAppearanceTransition(true, animated: false)
。
All your child VCs need to already be children of the parent before you call func beginAppearanceTransition(_: animated:)
在调用func beginAppearanceTransition(_: animated:)
之前,所有子 VC 都需要已经是父级的子级
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.