5

我使用了基于视图的应用程序,因为我以编程方式生成 TabBar。问题是:

I have an Iphone application in which i have 2 tabitems with a tabbarcontroller.Inside the tabbarcontroller each viewcontroller is a navigation controller.when selecting the second tab i have a view controller.when selecting a button on that i am pushing another view controller to the self.navigation 控制器。在那个视图控制器中,我正在推动并像那样前进。但问题是当我再次选择 tabitem 时,pushviewcotroller 显示在那里。但是当我选择选项卡时,我再次需要那个 rootview

我在 AppDelegate.m 中的代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

 UINavigationController *nc1;
    nc1 = [[UINavigationController alloc] init];

UIViewController *viewController1 = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];





    UINavigationController *nc2;
    nc2 = [[UINavigationController alloc] init];

 UIViewController *viewController2 = [[[secondview alloc] initWithNibName:@"secondview" bundle:nil] autorelease];
    nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];


    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
   self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1,nc2,nil];
  self.window.rootViewController=self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
4

4 回答 4

12

也许你正在寻找这个:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{       
    int tabitem = tabBarController.selectedIndex;
    [[tabBarController.viewControllers objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}
于 2012-08-27T13:27:17.020 回答
4

迅速你可以在你的UITabBarController课堂上这样做:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
    rootView.popToRootViewControllerAnimated(false)
}
于 2016-04-13T10:08:26.907 回答
0

我相信您将需要使用这两种方法:

UINavigationController: - popToRootViewControllerAnimated:

UITabBarControllerDelegate: tabBarController:didSelectViewController:

我在自己的程序中使用的方法是仅在屏幕上显示根视图控制器时显示选项卡栏。

于 2012-08-27T13:30:44.680 回答
0

添加UITabBarControllerDelegate到您的AppDelegate, 和didFinishLaunchingWithOptions方法中,将委托设置为UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self;. Then the delegate method - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewControllerwill be invoked when tabbar is selected.

于 2016-02-03T17:10:50.103 回答