3

我有一个标签栏应用程序,在应用程序的 5 个标签中的 4 个上有导航控制器。我的应用程序中有一个“重置应用程序”功能,可以清除所有数据等……我还希望将所有视图控制器弹出回其顶视图。我知道如何使用 popToRootViewControllerAnimated 为单个导航控制器弹出到根目录,但是是否可以弹出每个选项卡上的所有视图控制器?

4

5 回答 5

11

如果数组中的控制器是 UINavigationController 之类的,则需要通过 tabBarController 的 viewControllers 数组枚举并弹出到根视图控制器 -

for(UIViewController *viewController in tabBarController.viewControllers)
{
    if([viewController isKindOfClass:[UINavigationController class]])
       [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
于 2013-01-15T17:54:57.687 回答
1

拉胡尔的答案是完美的解决方案。但是,如果您的标签栏中有超过 5 个标签,那么您将看到“更多”标签。您将需要明确地重置此选项卡(只需将 tabBarController.moreNavigationController 弹出到 rootViewController)。

这是代码示例:

for(UIViewController *viewController in tabBarController.viewControllers)
{
    if([viewController isKindOfClass:[UINavigationController class]])
       [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
[tabBarController.moreNavigationController popToRootViewControllerAnimated:NO];

..

于 2013-12-25T22:16:19.120 回答
1

对于任何需要它的人来说,这里是 Swift 5:

if let tabVcs = navigationController?.tabBarController?.viewControllers {
        for vc in tabVcs {
            if let navVc = vc as? UINavigationController {
                navVc.popToRootViewController(animated: false)
            }
        }
    }
于 2020-06-26T02:23:33.613 回答
0

另一个选择是使用 NSNotifications。关闭模式视图(计时器上的幻灯片放映)时,我需要在所有选项卡上触发 popToRootViewController,这是我弄清楚如何做到这一点的唯一方法。我在模态视图的 viewWillDissapear 方法中触发了 NSNotification,然后在我希望关闭的每个视图中对其进行响应。

于 2013-07-05T04:00:40.557 回答
0
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)


    } 

}

我的代码在 Swift 中弹出所有选项卡的示例。这是在我的 App Delegate 中。这就是我在 VC 中的称呼

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.popAll()
于 2016-11-13T12:57:13.967 回答