【问题标题】:IPhone Dev: Activate the view of a tabBar buttoniPhone Dev:激活tabBar按钮的视图
【发布时间】:2010-11-15 04:47:54
【问题描述】:

我有一个应用程序,它有一个处理所有视图的 tabBar。在第一个视图中,我有一个登录过程。当该过程完成后,我想自动转到第二个 tabBar 视图,而不需要用户单击其各自的 tabBar 按钮。

到目前为止,我所要做的就是用这个突出显示按钮:

myTabBar.selectedItem = [myTabBar.items objectAtIndex:1];

但我不知道如何自动将与该按钮相关的第二个视图置于前面。到目前为止,用户必须在按钮亮起(选中)时按下按钮。

知道如何做到这一点吗?可能吗?将不胜感激。谢谢。

【问题讨论】:

    标签: iphone tabbar


    【解决方案1】:

    使用对应UITabBarControllerselectedViewControllerselectedIndex方法。

    作为对这个答案的 cmets 的回应,我提供了一个如何实现的示例:

    id firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]];
    id secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];
    
    [firstViewController release];
    [secondViewController release];
    
    // Select the second tab bar item and its view
    self.tabBarController.selectedIndex = 1;
    
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    在我的测试中,似乎使用 UITabBarController 的 selectedViewController 方法设置当前视图不会更新 selectedIndex 属性(显示新视图但未更改所选 UITabBarItem )。这与文档中承诺的行为相反。但是,使用上面代码 sn-p 中演示的 selectedIndex 方法应该可以正常工作。

    【讨论】:

    • 对不起,我很迷茫。我试过这些:rootController.selectedIndex = 1; rootController.selectedViewController.tabBarItem = [myTabBar.items objectAtIndex:1]; ... rootController 是 UITabBarController,它们都不起作用...你能告诉我如何使用这些方法来获得我想要的东西吗?谢谢。
    • 这更有意义,但也不起作用: self.rootController.selectedViewController = [self.rootController.viewControllers objectAtIndex:1];关于我做错了什么的任何线索?
    • 这... PickersAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; self.rootController.selectedViewController = delegate.navController;看来 selectedViewController 不适用于我的控制器:-S
    • 您确定 UITabBarController 已链接到相关的 UITabBar(通过其 tabBar 属性)?
    • 感谢您的回复,但它对我不起作用。也许问题是关于 tabBar 属性的事情,但我不知道你到底是什么意思......我有两个 viewController 链接到 tabBar 的第一个和第三个按钮,一个 navController 链接到第二个(我想要自动激活)我不明白为什么selectedIndex方法不适用于我的rootController(tabBar控制器)。您认为 IB 中缺少什么?我不知道发生了什么
    【解决方案2】:

    我终于在这里得到了解决方案:

    iphone sdk: set focus to different tabbar view on button click from first tabbar item

    还是谢谢。

    【讨论】:

    • 这正是我的建议:tabController.selectedIndex = 1;
    【解决方案3】:

    您应该实现 UITabBarControllerDelegate 协议,如下所述:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html

    要更改活动选项卡,请使用 tabBarController:shouldSelectViewController: 如文档中所述,“您可以使用此方法动态决定是否应将给定选项卡设为活动选项卡。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-08
      • 2012-05-10
      • 2021-07-30
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多