objective-c 以编程方式按下 Xcode 中的 UITabBar 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1795407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically pressing a UITabBar button in Xcode
提问by Magic Bullet Dave
Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.
对不起,新手问题。我的主窗口视图中有一个 UITabBar 以及每个选项卡的 UINavigationController 数组。该结构类似于 iPod 应用程序,因为可以通过选择 TabBar 项来查看主视图,然后用户可以通过将视图推送到堆栈来使用 NavigationController 进一步深入查看。
What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).
我希望能够做的是相当于从代码中的任何子视图按下底部的 TabBar 按钮(即,更改 TabBar 的 selected 属性并显示启动选项卡的第一个视图控制器) .
Any help would be greatly appreciated.
任何帮助将不胜感激。
Dave
戴夫
回答by prakash
[myTabBarController setSelectedIndex:index]
EDIT: Answering the part 2 question from the comment:
编辑:回答评论中的第 2 部分问题:
You can define a method in AppDelegate for switching to a different tab.
您可以在 AppDelegate 中定义一个方法来切换到不同的选项卡。
And you can get hold of appdelegate from anywhere and send a message.. something like:
您可以从任何地方获取 appdelegate 并发送消息.. 类似于:
MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
[appDelegate SwitchToTab:index]
回答by codemonkey
alternatively...
或者...
[self.parentViewController.tabBarController setSelectedIndex:3];
回答by Rich Joslin
I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.
我想回复 Prakash,但不知道如何回复。也许我会被阻止,直到我的分数上升。
Anyhow, I hope this helps someone:
无论如何,我希望这对某人有所帮助:
I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:
我按照普拉卡什说的去做,什么也没发生。这是因为为了获得指向我的应用程序委托的指针,我这样做了:
AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];
When I should have been doing this:
当我应该这样做时:
AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];
Newbie mistake.
新手错误。
回答by PawanShakya
For this, You just need to take UITabBar controller -
为此,您只需要使用 UITabBar 控制器 -
.h File -
.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];
than you can set index as per your needs
比您可以根据需要设置索引
self._pTabBarController.selectedIndex = 0; // as per your requirements
回答by BriOnH
[self.parentViewController.tabBarController setSelectedIndex:3];
Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.
为我选择了索引,但它只是将导航栏控制器的索引突出显示为活动索引,但是虽然它突出显示了该索引,但它实际上位于与 tabbarmenu 项目建议的不同的视图控制器上。
Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:
只是想补充一点,我从我的视图控制器中使用了它,它的表现就像有人真的按下了菜单项一样;从代码:
UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];
Thank you for this post/answers it helped out a lot in my project.
感谢您的这篇文章/答案,它对我的项目有很大帮助。
回答by realtimez
I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.
我想做类似的事情,但对于 XCode 6.4 iOS (8.4) setSelectedIndex 本身不会这样做。
Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:
将标签栏的视图控制器添加到列表中,然后在某些函数中使用类似以下内容,然后调用它:
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;
You might have similar code already inside your didSelectedItem..
您的 didSelectedItem 中可能已经有类似的代码。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item == self.firstTabBarItem)
// Right here
}
else if ...
}

