以编程方式在Xcode中按下UITabBar按钮

时间:2009-11-25 08:15:52

标签: iphone objective-c cocoa-touch uinavigationcontroller uitabbar

抱歉新手问题。我在主窗口视图中有一个UITabBar,每个Tab都有一个UINavigationControllers数组。该结构类似于iPod应用程序,通过选择TabBar项目可以看到主视图,然后用户可以通过将视图推送到堆栈来进一步向下钻取。

我希望能够做的就是在代码中的任何子视图底部按下TabBar按钮(即,更改TabBar的选定属性并显示启动第一个视图控制器标签)。

非常感谢任何帮助。

戴夫

6 个答案:

答案 0 :(得分:23)

[myTabBarController setSelectedIndex:index]

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

您可以在AppDelegate中定义一个方法,以切换到其他选项卡。

你可以从任何地方获取appdelegate并发送消息.. 类似的东西:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]

答案 1 :(得分:11)

...交替地

[self.parentViewController.tabBarController setSelectedIndex:3];

答案 2 :(得分:3)

我想回复普拉卡什,但无法弄清楚如何。也许我被阻止,直到我的分数上升。

无论如何,我希望这有助于某人:

我正在做普拉卡什说的事,并没有发生任何事情。这是因为要获得指向我的app委托的指针,我这样做了:

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

当我应该这样做时:

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

新手的错误。

答案 3 :(得分: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

答案 4 :(得分:0)

[self.parentViewController.tabBarController setSelectedIndex:3];

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

只是想补充说我从我的视图控制器中使用了它,它的表现就像有人按下了menuitem;来自代码:

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

感谢您在我的项目中帮助过这篇文章/答案。

答案 5 :(得分: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 ...
}