【问题标题】:Show specific TabBarItem after view is removed from SuperView从 SuperView 中删除视图后显示特定的 TabBarItem
【发布时间】:2012-06-27 10:58:12
【问题描述】:

HomeViewController - 视图有 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];
}

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

【问题讨论】:

  • 你不应该从超级视图中删除 self,你为什么要这样做?
  • 感谢您的评论。我只需要删除 HomeViewCONtroller 的视图,以便我可以看到 TabBarController 视图。这是正确的做法吗?

标签: iphone ios5 uitabbarcontroller


【解决方案1】:

请检查以下场景,

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

我认为这可能对你有用。

【讨论】:

  • 是的,我正在尝试获取正确的 selectedIndex,即“3”,即第四个标签栏项目。
【解决方案2】:

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

所以只需执行以下操作

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

【讨论】:

  • 其实HomeViewController并不是TabBarController的一部分。它是一个独立的 ViewController,用于在显示 TabBar 之前显示带有 2 个图像按钮的视图。当点击 HomeViewCONtroller 上的每个按钮时,我需要转到特定选项卡。
  • 不,正如我提到的 HomeBiewController 不是我的 TabBar 的一部分。无论如何,谢谢你的答案。我会尝试其他东西并将其发布在这里。干杯
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多