【问题标题】:ViewWillAppear Not called in tabbar items view controllerViewWillAppear 未在标签栏项目视图控制器中调用
【发布时间】:2018-06-04 08:22:39
【问题描述】:

我有结构:

*- TabBarViewController (根)

*-- 导航视图控制器

*---- ChatViewController

*-- 导航视图控制器

*---- 菜单视图控制器

当我切换标签栏项目时,(Chat, Menu) 中的 viewWillAppear 只调用一次,但在 NavigationVC 中每次切换时都会调用。

是否可以在Chat和Menu ViewControllers中切换item时自动调用viewWillAppeare?

super.viewWillAppear 在方法内部。

我的代码如下:

class TabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    let v1 = storyboard!.instantiateViewController(withIdentifier: "ChatViewController")
    let v2 = storyboard!.instantiateViewController(withIdentifier: "MenuViewController")
    viewControllers = [v1,v2]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

class ChatViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(self,#function)
    }
}

class MenuViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(self,#function)
    }
}

它适用于明确的新项目,但适用于旧项目(我正在工作,我有很多功能,不起作用)

StoryboardId 与Storyboard 中的 NavigationViewController 相关联

【问题讨论】:

  • 是的,有可能
  • 发布与tabbarController相关的代码

标签: ios swift uinavigationcontroller uitabbarcontroller uitabbar


【解决方案1】:

我发现问题: 在extension UINavigationController 我有方法

    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if !UserDefaults.standard.isUserPresented {
            navigationBar.barTintColor = .rgbColor(red: 43, green: 43, blue: 43, alpha: 1)
        } else {
            navigationBar.barTintColor = .rgbColor(red: 100, green: 100, blue: 100, alpha: 1)
        }
    }

这个块 viewWillAppear 在 NavigationBar 的子视图控制器中

【讨论】:

    【解决方案2】:

    您的问题不清楚将 viewcontroller 添加到 tabbarcontroller 和 navigation controller 。我在 storyboard 中创建了所有内容。视图将出现在视图控制器中:

     override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            print("Menu View will appear")
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            print("Chat View will appear")
        }
    

    当我切换时能够得到以下输出:

    Menu View will appear
    Chat View will appear
    Menu View will appear
    Chat View will appear
    

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2018-02-11
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多