12

对不起新手问题。我的主窗口视图中有一个 UITabBar 以及每个选项卡的 UINavigationControllers 数组。该结构类似于 iPod 应用程序,主要视图可以通过选择 TabBar 项目来查看,然后用户可以通过将视图推送到堆栈来使用 NavigationController 进一步深入。

我想做的是相当于从代码中的任何子视图按下底部的 TabBar 按钮(即,更改 TabBar 的 selected 属性并显示启动选项卡的第一个视图控制器) .

任何帮助将不胜感激。

戴夫

4

6 回答 6

23
[myTabBarController setSelectedIndex:index]

编辑:从评论中回答第 2 部分的问题:

您可以在 AppDelegate 中定义一个方法来切换到不同的选项卡。

您可以从任何地方获取 appdelegate 并发送消息.. 类似于:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]
于 2009-11-25T08:32:16.147 回答
11

或者...

[self.parentViewController.tabBarController setSelectedIndex:3];
于 2010-06-10T19:41:02.670 回答
3

我想回复 Prakash,但不知道如何回复。也许我被封锁了,直到我的分数上升。

无论如何,我希望这可以帮助某人:

我按照普拉卡什所说的去做,但什么也没发生。这是因为要获得指向我的应用程序委托的指针,我正在这样做:

AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];

当我应该这样做时:

AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];

新手误区。

于 2010-07-03T06:05:35.670 回答
0

为此,您只需要使用 UITabBar 控制器 -

.h 文件 -

UITabBarController *_pTabBarController;
@property (nonatomic, retain) IBOutlet  UITabBarController *_pTabBarController;

.m File -
// synthesize it 
@synthesize  _pTabBarController;    

At initial load 

// You can  write one function to add tabBar - 

// As you have already mentioned you have created an array , if not 

_pTabBarController = [[UITabBarController alloc] init];
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
    UINavigationController *theNavigationController;

    _pController = [[Controller alloc] initStart];  
    _pController.tabBarItem.tag = 1;
    _pController.title = @"Baranches";
    theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
    theNavigationController.tabBarItem.tag = 1;
    theNavigationController.tabBarItem.image = [UIImage imageNamed:@"icon_branches.png"];
    [localViewControllersArray addObject:theNavigationController];
    [theNavigationController release];

比您可以根据需要设置索引

self._pTabBarController.selectedIndex = 0; // as per your requirements
于 2011-10-12T07:19:40.837 回答
0
[self.parentViewController.tabBarController setSelectedIndex:3];

为我选择了索引,但它只是突出显示了 navbarcontroller 的索引作为活动索引,但是当它突出显示该索引时,它实际上位于与 tabbarmenu 项建议的不同的视图控制器上。

只是想补充一点,我从我的视图控制器中使用了它,它的表现就像有人实际按下了菜单项一样;来自代码:

UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];

感谢您的这篇文章/答案,它对我的​​项目有很大帮助。

于 2012-06-15T23:20:03.390 回答
0

我想做类似的事情,但对于 XCode 6.4 iOS (8.4) setSelectedIndex 本身不会这样做。

将标签栏的视图控制器添加到列表中,然后在某些函数中使用类似以下内容,然后调用它:

FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview]
[self.view insertSubview:firstVC.view belowSubview:self.tabBar];
[self.tabBar setSelectedItem:self.firstTabBarItem];
self.selectedViewController = firstVC;

您的 didSelectedItem.. 中可能已经有类似的代码。

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

   if (item == self.firstTabBarItem) 
      // Right here
    }
    else if ...
}
于 2015-07-29T19:31:04.483 回答