把所有细节视图切换时关注TabBarControllerDismiss all Detail Views when switching Tabs on TabBarController

- 此内容更新于:2015-01-27
主题:

原文:

My iOS app has:

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

My Ap Behavior:

  • When the app loads, I see the TableView1.
  • I select an Item in the table, and it takes me via Show (Push) segue the details view 1.
  • I switch to the second tab on the bottom, and see TableView2.
  • I select an item and it takes me to details view 2
  • I navigate back to first tab, and see details view 1

Desired:

When performing last step, I'd like to dismiss the details view and see the first TableView1, and when switching back to second tab, I want that one to be dismissed and to see the table view.

I've tried different combinations of dismissViewControllerAnimated and popToRootViewControllerAnimated but I just don't seem to figure it out.

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。监听修改选项卡选择。基于新标签,标签的导航控制器和调用它的popToRootViewControllerAnimated:方法。
原文:

One option is to make use of the UITabBarControllerDelegate. Listen for changes to the tab selection. Based on the new tab, get the tab's navigation controller and call its popToRootViewControllerAnimated: method.

Dmytro的回复:

(原文:This is exactly what I was trying to do, but it just doesn't seem to pop back to root view controller. The didSelectViewController method works fine, I get the proper NSLog output, but the popToRootViewControllerAnimated just doesn't do anything. I tried calling it on several different objects available in the scope of the function, but nothing worked :( Now I'm not sure I even declared the delegate properly...)

rmaddy的回复:更新你的问题与相关的代码。然后有人可以帮助你找到这个问题。

(原文:Update your question with the relevant code. Then someone can help you find the issue.)

解决方案:
您可以直接设置视图控制器目前导航堆栈上。你要直接设置viewcontroller属性的导航控制器切换选项卡时tabbar控制器。 NavigationController1设置。viewcontroller = @[tableView1]当你切换到tab1
原文:

You can directly set the view controllers currently on the navigation stack. All you have to is directly set the viewControllers property of the navigation controllers when switch tabs in the tabbar controller.

Set NavigationController1.viewcontrollers = @[tableView1] when you switch to tab1

解决方案:
这是一个简单的解决方案。 努力实现以下UIViewContorller的方法 去你的细节1视图控制器,实现的方法-(void)viewWillDisappear:(保龄球)动画。 做一个流行的控制器。 你应该做节点2相同 能帮你解决,目的是正确的方法。 希望有帮助。
原文:

Here is a simple solution for this.

Try to implement the following methods of UIViewContorller

- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing
- (void)viewDidDisappear:(BOOL)animated;  // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
  1. Go to your detail-1 view controller and implement the method - (void)viewWillDisappear:(BOOL)animated. Do a pop for that controller.

  2. Same you should do for the detail-2

That should solve your purpose & is the right approach.

Hope that helps.