[英]Popping all View Controllers on Tab Bar App
I have a tab bar app with navigation controllers on 4 out of the 5 tabs on the app.我有一个标签栏应用程序,该应用程序的 5 个选项卡中有 4 个带有导航控制器。 I have a 'reset app' function within my app which clears all data etc... and I would also like that to pop all of the view controllers back to their top view.
我的应用程序中有一个“重置应用程序”功能,它可以清除所有数据等……而且我还希望将所有视图控制器弹出回它们的顶视图。 I know how to pop to root using popToRootViewControllerAnimated for a single nav controller but is it possible to pop all of the view controllers on each tab?
我知道如何使用 popToRootViewControllerAnimated 为单个导航控制器弹出根目录,但是否可以弹出每个选项卡上的所有视图控制器?
You need to enumerate through viewControllers array of tabBarController & pop to root view controller if controller in array is UINavigationController like-如果数组中的控制器是 UINavigationController 之类的,则需要通过 tabBarController 和 pop 的 viewControllers 数组枚举到根视图控制器 -
for(UIViewController *viewController in tabBarController.viewControllers)
{
if([viewController isKindOfClass:[UINavigationController class]])
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
Rahul's answer is perfect solution.拉胡尔的回答是完美的解决方案。 But if there are more than 5 tabs in your tabbar then you will see the "more" tab.
但是如果您的标签栏中有超过 5 个标签,那么您将看到“更多”标签。 you will need to reset this tab explicitly (just pop tabBarController.moreNavigationController to rootViewController).
您将需要显式重置此选项卡(只需将 tabBarController.moreNavigationController 弹出到 rootViewController)。
here is code example:这是代码示例:
for(UIViewController *viewController in tabBarController.viewControllers)
{
if([viewController isKindOfClass:[UINavigationController class]])
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
[tabBarController.moreNavigationController popToRootViewControllerAnimated:NO];
Here's Swift 5 for anyone that needs it:这是适用于任何需要它的人的 Swift 5:
if let tabVcs = navigationController?.tabBarController?.viewControllers {
for vc in tabVcs {
if let navVc = vc as? UINavigationController {
navVc.popToRootViewController(animated: false)
}
}
}
Another option you have is to use NSNotifications.您的另一个选择是使用 NSNotifications。 I needed to trigger a popToRootViewController on all of my tabs when closing a modal view (slide show on timer) and this is the only way I could figure out how to do it.
关闭模态视图(计时器上的幻灯片)时,我需要在所有选项卡上触发 popToRootViewController,这是我弄清楚如何执行此操作的唯一方法。 I triggered the NSNotification in viewWillDissapear method of modal view and then responded to it in each view I wished to close.
我在模态视图的 viewWillDissapear 方法中触发了 NSNotification,然后在我希望关闭的每个视图中响应它。
func popAll(){
let tabBarController = window!.rootViewController as! UITabBarController
tabBarController.delegate = self
if let tabBarViewControllers = tabBarController.viewControllers {
let campusController = tabBarViewControllers[0] as! UINavigationController
let campusTVC = campusController.viewControllers[0] as! CampusTVC
_ = campusTVC.navigationController?.popToRootViewController(animated: false)
let adController = tabBarViewControllers[1] as! UINavigationController
let adminTVC = adminController.viewControllers[0] as! AdTVC
_ = adminTVC.navigationController?.popToRootViewController(animated: false)
let searchController = tabBarViewControllers[2] as! UINavigationController
let searchTVC = searchController.viewControllers[0] as! SearchTVC
_ = searchTVC.navigationController?.popToRootViewController(animated: false)
}
}
Example of my code popping all tabs in Swift.我的代码在 Swift 中弹出所有选项卡的示例。 This is in my App Delegate.
这是在我的应用程序委托中。 This is how I call it in my VC
这就是我在 VC 中的称呼
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.popAll()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.