从SuperView中删除视图后显示特定的TabBarItem

时间:2012-06-27 10:58:12

标签: iphone ios5 uitabbarcontroller

HomeViewController - View有2个图像按钮,名为“New”& '旧'。这是我在TabBarController进入图片之前显示的起始视图。

点击“新建”时,我会转到TabBarItem 1.好的,没问题。 * 当点击'Old'时,我想转到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];
}

的AppDelegate:

- (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;
  }

3 个答案:

答案 0 :(得分:0)

请检查以下情况,

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

我认为这可能对你有用。

答案 1 :(得分:0)

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

所以,请执行以下操作

//[self.view removeFromSuperview];
[self.tabBarController  setSelectedIndex:3]; //3 size index starts from zero and you need the 4th controller

答案 2 :(得分:0)

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

 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];

请检查一下,您是否按照所有步骤进行操作?我在上面的代码中已经遵循了这一点。