我有结构:
* - TabBarViewController(Root)
* - NavigationViewController
* ---- ChatViewController
* - NavigationViewController
* ---- MenuViewController
当我切换tabbar项目时,(Chat,Menu)中的viewWillAppear只调用一次,但每次切换时都会调用NavigationVC。
切换项目时是否可以在聊天和菜单ViewControllers中自动调用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
答案 0 :(得分:0)
您的问题不明确是将viewcontroller添加到tabbarcontroller和导航控制器。我在故事板中创建了所有内容。 View将出现在视图控制器中:
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
答案 1 :(得分:0)
我发现了问题:
在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中的子视图控制器中