简体   繁体   English

由于我的视图层次结构,显然没有调用 viewWillAppear

[英]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.

相关问题 为什么最初显示我的视图控制器时viewWillAppear:被调用两次? - Why is viewWillAppear: called twice when my view controller is initially displayed? 当应用程序处于活动状态时,在我的视图 controller 中不会调用 viewWillAppear - viewWillAppear is not called in my view controller when the app becomes active 在UISplitViewController的主视图中未调用viewWillAppear - viewWillAppear is not called in the master view of UISplitViewController viewWillAppear与loadView用于构建视图层次结构 - viewWillAppear versus loadView for building view hierarchy iOS viewWillAppear错误:“视图不在窗口层次结构中” - iOS viewWillAppear Error: “view is not in the window hierarchy” 为什么在我包含的视图控制器上的viewWillAppear:从transitionFromViewController调用了两次? - Why is viewWillAppear: on my contained view controller being called twice from transitionFromViewController? ViewWillAppear在标签栏项视图控制器中未调用 - ViewWillAppear Not called in tabbar items view controller viewWillAppear没有被调用 - viewWillAppear is not called 如何中断viewWillAppear层次结构 - How to interrupt viewWillAppear hierarchy 纵向时在主视图控制器中未调用SplitViewController和viewWillAppear的问题 - Issue with SplitViewController and viewWillAppear not called in Master View Controller when in portrait
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM