【发布时间】:2021-06-01 08:00:38
【问题描述】:
Tab1 -> Nav1 -> Root1VC -> Child1VC
Tab2 -> Nav2 -> Root2VC -> Child2VC
在 Root1VC 中,我根据用户的登录状态显示不同的内容。
假设我访问了 Child1VC,然后点击了 Tab2,在 Tab2 上我点击了登录。此时,我想关闭 Nav1 堆栈中所有呈现的视图并返回到 Nav1 的根并在 Root1VC 中调用一个函数。怎么做?
我有以下代码,它有效,但我不确定这是否是最好的方法。
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let tabBar:UITabBarController = appDelegate.window?.rootViewController as? UITabBarController {
if let menuNav:UINavigationController = tabBar.viewControllers?[2] as? UINavigationController {
var topPresentedVC: UIViewController!
if let topVC = menuNav.topViewController {
topPresentedVC = topVC
} else if let presentedVC = menuNav.presentedViewController {
topPresentedVC = presentedVC
}
if topPresentedVC != nil {
if !topPresentedVC.isKind(of: MoreViewController.classForCoder()) {
topPresentedVC.navigationController?.popToRootViewController(animated: false)
}
if let moreVC = menuNav.viewControllers.first as? MoreViewController {
moreVC.initMenus()
} else {
print("Can't find first VC")
}
}
}
}
【问题讨论】:
标签: ios swift uinavigationcontroller uitabbarcontroller