ios 如何使用 StoryBoard 在 UITabBarController 中设置选定的选项卡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17919302/
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
How do I set selected tab in UITabBarController using StoryBoard?
提问by Matrosov Alexander
How can I switch to some tab in UITabBarController
using StoryBoard? I have tried the code below but without success (the tab is not selected):
如何在UITabBarController
使用 StoryBoard 时切换到某个选项卡?我尝试了下面的代码但没有成功(未选择选项卡):
self.tabBar.selectedIndex = 3;
Honestly I used nib files without StoryBoard and this code above worked fine in
老实说,我使用了没有 StoryBoard 的 nib 文件,上面的代码在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
:(NSDictionary *)launchOptions
but now I can't set the tab programatically. Maybe there is another problem that is not connected to selecting the tab. How can I switch tabs?
但现在我无法以编程方式设置选项卡。也许还有另一个与选择选项卡无关的问题。如何切换标签页?
回答by Tommy Devoy
Grab your instance of UITabBarController
then set the selectedViewController
property:
抓住你的实例UITabBarController
然后设置selectedViewController
属性:
yourTabBarController.selectedViewController=[yourTabBarController.viewControllers objectAtIndex:3];//or whichever index you want
回答by Yanchi
Alexander, I think your problem is getting correct instance of your tab bar. If your tab bar is your root view controller, then you can do it like this in your appdelegate if didFinishLoading method:
亚历山大,我认为您的问题是获取标签栏的正确实例。如果你的标签栏是你的根视图控制器,那么你可以在你的 appdelegate if didFinishLoading 方法中这样做:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:3];
Give it a try and tell me the result please.
试一试,请告诉我结果。
回答by Phil Andrews
*Swift Comment - 18 months later if you convert Yanchi's solution into Swift in your appDelegate you'll get the expected result. The Swift translation is:
*Swift Comment - 18 个月后,如果您在 appDelegate 中将 Yanchi 的解决方案转换为 Swift,您将获得预期的结果。Swift 的翻译是:
let tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 1
回答by iosLearner
This will work in stroryboard too...
这也适用于故事板......
[self.tabBarController setSelectedIndex:3];
with this add UITabBarControllerDelegate
in .h
[self.tabBarController setSelectedIndex:3];
这个附加UITabBarControllerDelegate
的.h
and then use this delegate
method
然后使用这个delegate
方法
- (BOOL)tabBarController:(UITabBarController *)theTabBarController shouldSelectViewController:(UIViewController *)viewController
{
return (theTabBarController.selectedViewController != viewController);
}
回答by Wubbe
You can achieve this also using storyboards only. Ctrl-drag from the tabBarController to your ViewController(s), and select 'Relationship Segue, View controllers'. The first ViewController you select will be the default/initial ViewController.
您也可以仅使用情节提要来实现这一点。按住 Ctrl 键从 tabBarController 拖动到您的 ViewController(s),然后选择“Relationship Segue, View controllers”。您选择的第一个 ViewController 将是默认/初始 ViewController。
回答by superarts.org
I'm using IBInspected in LSwift
:
我正在使用 IBInspected LSwift
:
extension UITabBarController {
@IBInspectable var selected_index: Int {
get {
return selectedIndex
}
set(index) {
selectedIndex = index
}
}
}
回答by Ketan Sodvadiya
Swift 4.1
斯威夫特 4.1
In your TabBarViewController
class, you can add this key line
在您的TabBarViewController
班级中,您可以添加此关键行
self.selectedIndex = 0
回答by amirhashemei
You can also set the default tab in "User Defined Runtime Attributes"using storyboard.
您还可以使用情节提要在“用户定义的运行时属性”中设置默认选项卡。
Select your Tab Bar Controller from storyboard, in the right pane select Identity Inspector and add an attribute:
从情节提要中选择您的 Tab Bar Controller,在右侧窗格中选择 Identity Inspector 并添加一个属性:
Key Path : selectedIndex Type : Number Value : 2 ( whatever number you want )