在所选标签栏项目上点击两次隐藏当前视图控制器

时间:2013-10-14 20:46:43

标签: iphone ios objective-c uiviewcontroller pushviewcontroller

我在使用导航堆栈推送视图时遇到了一些问题。

我遇到的问题是,在触摸标签栏项后,视图控制器被推入导航堆栈(来自名为FirstViewController的视图控制器),如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    svc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:svc animated:YES];
}

按预期工作,但是当再次触摸相同的标签栏项时会出现实际问题。

当发生这种情况时,当前视图(先前推送的SecondViewController)被删除,就像我将触摸“完成”按钮一样。

我无法追踪发生的地点或原因。

编辑:这是我设置标签栏,查看控制器和导航的方式:

@implementation AppDelegate
@synthesize HomeViewController, FirstViewController, SecondViewController,     ThirdViewController, SettingsViewController, tabBarController, window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    FirstViewController *firstViewController = [[FirstViewController alloc]
                                            initWithNibName:nil bundle:nil];
    UINavigationController *firstViewControllerNav = [[UINavigationController alloc]
                                      initWithRootViewController:firstViewController];

    SecondViewController *secondViewController = [[SecondViewController alloc]
                                             initWithNibName:nil bundle:nil];
    UINavigationController *secondViewControllerNav = [[UINavigationController alloc]
                                                      initWithRootViewController:secondViewController];

    ThirdViewController *thirdViewController = [[ThirdViewController alloc]
                                                  initWithNibName:nil bundle:nil];
    UINavigationController *thirdViewControllerNav = [[UINavigationController alloc]
                                                      initWithRootViewController:thirdViewController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[firstViewControllerNav,
                                              secondViewControllerNav];

    UITabBar *tabBar = tabBarController.tabBar;

    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];

    [self.window setRootViewController:self.tabBarController];

    [self.window makeKeyAndVisible];

    return YES;
}

1 个答案:

答案 0 :(得分:12)

触摸标签栏项两次将导致导航控制器弹回到根视图控制器。这是预期和内在的行为。

为防止出现这种情况,您必须使用tabBarController:shouldSelectViewController:

UITabBarControllerDelegate protocol.方法

我相信这样的事情可以解决问题:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}