ios - 在 TabBarController 上切换选项卡时关闭所有详细 View

标签 ios objective-c uitabbarcontroller

我的 iOS 应用有:

TabBarController
    NavigationController1
        TableView1
            ViewController1 (Details View)
    NavigationController2
        TableView2
            ViewController2 (Details View)

My Ap 行为:

  • 当应用加载时,我看到了 TableView1。
  • 我在表中选择一个项目,它通过显示(推送)将我带到详细信息 View 1。
  • 我切换到底部的第二个选项卡,然后看到 TableView2。
  • 我选择了一个项目,它将我带到详细信息 View 2
  • 我导航回第一个选项卡,并查看详细信息 View 1

期望:

执行最后一步时,我想关闭详细信息 View 并查看第一个 TableView1,而当切换回第二个选项卡时,我希望关闭该 View 并查看表格 View 。

我已经尝试了 dismissViewControllerAnimatedpopToRootViewControllerAnimated 的不同组合,但我似乎没有弄明白。

MainTabBarController.h

@interface MainTabBarController : UITabBarController <UITabBarControllerDelegate>

MainTabBarController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}
...
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    // NSLog Works fine, and displays information in the output
    NSLog (@"%@ %lu", tabBarController.selectedViewController.title, tabBarController.selectedIndex);

    // None of the lines below achieve the desired result
    [viewController.navigationController popToRootViewControllerAnimated:YES];
    [viewController dismissViewControllerAnimated:YES completion:nil];

    [tabBarController.navigationController popToRootViewControllerAnimated:YES];
    [tabBarController dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

一种选择是使用 UITabBarControllerDelegate。监听选项卡选择的变化。基于新选项卡,获取选项卡的导航 Controller 并调用其 popToRootViewControllerAnimated: 方法。

根据添加到问题的代码更新:

问题在于您尝试弹出 View Controller 的方式。你想要这个:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    // NSLog Works fine, and displays information in the output
    NSLog (@"%@ %lu", tabBarController.selectedViewController.title, tabBarController.selectedIndex);

    // If the selected tab's root controller is a navigation controller
    // pop to the top view controller in the tab's navigation stack
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)viewController;
        [nav popToRootViewControllerAnimated:NO];
    }
}

关于ios - 在 TabBarController 上切换选项卡时关闭所有详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145271/

相关文章:

ios - 对 <UITabBarController : 0x197870> 的开始/结束外观转换的不平衡调用

swift 3 : I can't hide the back button

ios - 将多个数组放入字典

iphone - 将新地点添加到 Google 地方信息

ios - 你如何在 Xcode 中声明一个全局变量?

ios - 图标角标(Badge)未出现在 IOS 10 模拟器中

ios - iOS 中的 UIAlertView 委托(delegate)协议(protocol)

ios - 当网络连接缓慢时应用程序卡住,因为 POST API 调用发生在应用程序启动时。下面是我的连接管理器代码,Objective-C,iOS

iphone - 获取 nsdictionary 中特定键的值

ios - Swift 显示了两个标签栏,但不确定为什么?