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

Posted

技术标签:

【中文标题】由于我的视图层次结构,显然没有调用 viewWillAppear【英文标题】:viewWillAppear not called apparently due to my view hierarchy 【发布时间】:2021-03-01 02:44:22 【问题描述】:

这似乎是一个有据可查的问题,但在线解决方案没有奏效。 以下是未能为我提供有效答案的帖子示例列表:

ViewWillAppear not executing code viewWillAppear not getting called viewWillAppear not called UINavigationController Inheritance, ViewWillAppear not called viewWillAppear not called after popToViewController iPhone viewWillAppear not firing

我发现我的 viewWillAppear 没有被调用的问题与我的视图层次结构有关。我使用的选项卡控制器不是视图层次结构的最高部分。选项卡控制器的视图控制器之一是导航控制器的根视图控制器。这就是我试图获得工作 viewWillAppear 或 viewDidAppear 的地方。这是我尝试过的,但没有奏效。在标签控制器中,我添加了以下代码:

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)
    

在场景委托中,这是我的代码:

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)

【问题讨论】:

【参考方案1】:

尝试在rootVC.beginAppearanceTransition(true, animated: false) 之前调用rootVC.add(asChildViewController: tbc)

在您致电func beginAppearanceTransition(_: animated:)之前,您的所有子 VC 必须已经是父级的子级

【讨论】:

以上是关于由于我的视图层次结构,显然没有调用 viewWillAppear的主要内容,如果未能解决你的问题,请参考以下文章

调用具有多个子视图层次结构的协议方法

视图层次结构没有为约束做好准备?

XCode Interface builder缺少视图层次结构

视图层次结构没有为约束做好准备

视图层次结构没有为约束做好准备:无法找到问题

“视图层次结构没有为约束做好准备”错误 Swift 3