ios 使用故事板时设置默认选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13136699/
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
Setting the default tab when using storyboards
提问by user1145581
Can anyone please tell me how to set the default tab when using storyboards in iOS. I can't seem to figure out how to accomplish this.
谁能告诉我在 iOS 中使用故事板时如何设置默认选项卡。我似乎无法弄清楚如何实现这一点。
Thank you
谢谢
采纳答案by Trevor Rudolph
You can use one of these two methods:
您可以使用以下两种方法之一:
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
or a direct method from the object
或来自对象的直接方法
[tabBar setSelectedItem:myUITabBarItem];
or you can combine them to do this:
或者你可以组合它们来做到这一点:
tabBar.items = tabBarItems;
[tabBar setSelectedItem:[tabBarItems objectAtIndex:0]];
but i havent tested that method yet, hope this helps!
但我还没有测试过这种方法,希望这有帮助!
回答by Joshua Finch
Whilst you can set the initial selected tab programmatically like the other answers, to achieve the same in your storyboard without touching code you would perform the following:
虽然您可以像其他答案一样以编程方式设置初始选定的选项卡,但要在故事板中实现相同的效果而无需接触代码,您将执行以下操作:
- Select the Tab Bar Controller in the Storyboard Interface
- Show the Identity Inspector in the Utilities panel
- Add a new "User Defined Runtime Attribute"
- Set the Key Path to "selectedIndex"
- Set the Type to "Number"
- Set the Value to the index of the tab you wish to select (a value of 1 would select the second tab for example)
- Save the Storyboard, build and run the application
- 在 Storyboard 界面中选择 Tab Bar Controller
- 在“实用工具”面板中显示身份检查器
- 添加新的“用户定义的运行时属性”
- 将键路径设置为“selectedIndex”
- 将类型设置为“数字”
- 将值设置为您要选择的选项卡的索引(例如,值为 1 将选择第二个选项卡)
- 保存 Storyboard,构建并运行应用程序
This should be what it looks like when you've achieved the above steps:
当您完成上述步骤时,它应该是这样的:
回答by Aviel Gross
Might seem like overkill for some to subclass UITabBarController
, but, I think it provides the cleanest solution.
对于某些人来说,子类化似乎有点矫枉过正UITabBarController
,但是,我认为它提供了最干净的解决方案。
- Create
BaseTabBarController.swift
Add an
@IBInspectable
and set it inviewDidLoad
:class BaseTabBarController: UITabBarController { @IBInspectable var defaultIndex: Int = 0 override func viewDidLoad() { super.viewDidLoad() selectedIndex = defaultIndex } }
In the storyboard, set you
UITabBarController
to be your new subclass:
- 创建
BaseTabBarController.swift
添加一个
@IBInspectable
并将其设置为viewDidLoad
:class BaseTabBarController: UITabBarController { @IBInspectable var defaultIndex: Int = 0 override func viewDidLoad() { super.viewDidLoad() selectedIndex = defaultIndex } }
在故事板中,将您
UITabBarController
设置为您的新子类:
- Go to the Attributes Inspectoradd set the new property Default Index:
- 转到Attributes Inspector添加设置新属性Default Index:
- ta-da! (:
- 达达!(:
回答by DevonDahon
- Create a new file subclass of
UITabBarController
; Add this at the end of
viewDidLoad
:self.selectedIndex = 1;
Set this new file as the Custom Class in the
UITabBarController
of your Storyboard.
- 创建一个新的文件子类
UITabBarController
; 在末尾添加
viewDidLoad
:self.selectedIndex = 1;
将此新文件设置为
UITabBarController
Storyboard 中的自定义类。
You're done.
你完成了。
回答by Rahul Singh
The following code worked for me:
以下代码对我有用:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 2;
回答by zizutg
in appdelegate find applicationDidBecomeActive
function and add this lines
在 appdelegate 中查找applicationDidBecomeActive
函数并添加此行
let tabBarController = self.window?.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0 // any index you want
回答by jake
In the viewDidLoad()
of the TabBarController
, set selectedIndex
to whatever you want. (0
would be the first, 3
would be the fourth, etc.)
在viewDidLoad()
中TabBarController
,设置selectedIndex
为您想要的任何内容。(0
将是第一个,3
将是第四个,以此类推)
回答by WINSergey
My variant is suitable when you want just change the default selected controller, no more customizing. Just add the follow category:
当您只想更改默认选择的控制器而不需要更多自定义时,我的变体适用。只需添加以下类别:
// UITabBarController+DefaultPage.h
#import <UIKit/UIKit.h>
@interface UITabBarController(DefaultPage)
@end
// UITabBarController+DefaultPage.m
#import "UITabBarController+DefaultPage.h"
@implementation UITabBarController(DefaultPage)
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedIndex = 1;
}
@end
p.s: I prefer @joshua-finch answer
ps:我更喜欢@joshua-finch答案
回答by mohammad alnajjar
You can achieve this through Xcode 8 or later (just tested it and don't know if it's available before this version)
你可以通过Xcode 8或更高版本来实现(只是测试过,不知道在这个版本之前是否可用)
Do the steps as @Joshua Finch said but:
执行@Joshua Finch 所说的步骤,但是:
- Select the bar item instead of the TabBar
- Got to "User Defined Runtime Attribute"
- Add new key
- Name it "selected"
- Set its type to boolean and choose true / or check the checkbox
- 选择栏项目而不是 TabBar
- 转到“用户定义的运行时属性”
- 添加新密钥
- 将其命名为“选定”
- 将其类型设置为 boolean 并选择 true / 或选中复选框
回答by ThomasHaz
Building upon Aviel Gross' answer, I just wanted to implement this for a UITabBar, as opposed to a UITabBarController. This can be done as follows:
基于 Aviel Gross 的回答,我只想为 UITabBar 实现这个,而不是为 UITabBarController。这可以按如下方式完成:
class BaseTabBar: UITabBar {
@IBInspectable var defaultIndex: Int = 0 {
didSet {
self.selectedItem = self.items?[defaultIndex]
}
}
}