0

HomeViewController - View 有 2 个图像按钮,分别称为“新”和“旧”。这是我在 TabBarController 启动之前显示的起始视图。

当点击“新建”时,我转到 TabBarItem 1。好的,没问题。*当点击“旧”时,我想转到 TabBarItem 4。 *但它仍然转到 TabBarItem 1。

这就是我的代码的样子:

在 HomeViewController 中,我有以下方法:

- (void) oldButtonPressed:(id)sender{
TabBarAppDelegate *allRootValues = [[UIApplication sharedApplication] delegate];
allRootValues.seeExistingClients = @"Y";
NSLog(@"old button pressed: see old clients: %@", allRootValues.seeExistingClients);

[self.view removeFromSuperview];
[self.tabBarController  setSelectedIndex:4];
}

应用委托:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:       (NSDictionary *)launchOptions
{
    // Override point for customization after application launch.


    HomeViewController *homeVC = [[HomeViewController  alloc]initWithNibName:@"HomeViewController" bundle:nil];  
    [self.window addSubview:homeVC.view];
    [self.window makeKeyAndVisible];

    seeExistingClients = @"N"; //Assigning to 'N' initially

    return YES;
  }
4

3 回答 3

0

请检查以下场景,

 check the selected index which you are setting for selectedIndex:. The index for the controllers will be assigned from zero.

我认为这可能对你有用。

于 2012-06-27T11:28:12.993 回答
0

不要从 中删除 self.view HomeViewController,我不确定您的视图结构,但我认为这HomeViewController是您的一部分TabBarController

所以只需执行以下操作

//[self.view removeFromSuperview];
[self.tabBarController  setSelectedIndex:3]; //3 size index starts from zero and you need the 4th controller
于 2012-06-27T11:32:10.337 回答
0

你可以按如下方式创建标签栏控制器对象,

 UITabBarController *tabBarController = [[UITabBarController alloc] init];

创建UITabBarController的对象后,创建所有视图控制器的引用。示例代码如下,

FirstViewController *fisrtCont = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondCont = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

创建实例后,将所有对象添加到数组中。

NSArray *array = [[NSArray alloc] initWithObjects:fisrtCont, secondCont, nil];
tabBarController.viewControllers = array;

AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[delegate.window setRootViewController:controller];

[tabBarController setSelectedIndex:3];

请检查一下,您是否遵循所有步骤?我在上面的代码中遵循了这一点。

于 2012-06-27T12:13:02.780 回答