7

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

我遇到的问题是,在触摸标签栏项目后,视图控制器被推入导航堆栈(来自名为 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;
}
4

1 回答 1

12

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

为了防止这种情况,您必须使用UITabBarControllerDelegate 协议的tabBarController:shouldSelectViewController:方法。

我相信这样的事情会奏效:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}
于 2013-10-14T21:12:16.773 回答