[英]How do I set selected tab in UITabBarController using StoryBoard?
How can I switch to some tab in UITabBarController
using StoryBoard?如何使用 StoryBoard 切换到
UITabBarController
某个选项卡? I have tried the code below but without success (the tab is not selected):我尝试了下面的代码但没有成功(未选择选项卡):
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?
如何切换标签页?
获取您的UITabBarController
实例,然后设置selectedViewController
属性:
yourTabBarController.selectedViewController=[yourTabBarController.viewControllers objectAtIndex:3];//or whichever index you want
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.试一试,请告诉我结果。
*Swift Comment - 18 months later if you convert Yanchi's solution into Swift in your appDelegate you'll get the expected result. *Swift Comment - 18 个月后,如果您在 appDelegate 中将 Yanchi 的解决方案转换为 Swift,您将获得预期的结果。 The Swift translation is:
Swift 的翻译是:
let tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 1
This will work in stroryboard too...这也适用于故事板......
[self.tabBarController setSelectedIndex:3];
with this add UITabBarControllerDelegate
in .h
与此添加
UITabBarControllerDelegate
在.h
and then use this delegate
method然后使用这个
delegate
方法
- (BOOL)tabBarController:(UITabBarController *)theTabBarController shouldSelectViewController:(UIViewController *)viewController
{
return (theTabBarController.selectedViewController != viewController);
}
You can achieve this also using storyboards only.您也可以仅使用情节提要来实现这一点。 Ctrl-drag from the tabBarController to your ViewController(s), and select 'Relationship Segue, View controllers'.
按住 Ctrl 从 tabBarController 拖动到您的 ViewController(s),然后选择“Relationship Segue, View controllers”。 The first ViewController you select will be the default/initial ViewController.
您选择的第一个 ViewController 将是默认/初始 ViewController。
I'm using IBInspected in LSwift
:我在 LSwift 中使用
LSwift
:
extension UITabBarController {
@IBInspectable var selected_index: Int {
get {
return selectedIndex
}
set(index) {
selectedIndex = index
}
}
}
Swift 4.1斯威夫特 4.1
In your TabBarViewController
class, you can add this key line在您的
TabBarViewController
类中,您可以添加此关键行
self.selectedIndex = 0
Here is what I do (Swift 5.x):这是我所做的(Swift 5.x):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let tabBarController = self.window?.rootViewController as? UITabBarController {
if let viewControllers: [UIViewController] = tabBarController.viewControllers {
tabBarController.selectedIndex = viewControllers.count-1
}
}
return true
}
Swift 5.3斯威夫特 5.3
Let tabBar
be an instance of UITabBarController
then :让
tabBar
成为UITabBarController
一个实例,然后:
tabBar.selectedViewController = tabBar.viewControllers![2]
Note:笔记:
Instead of 2, put your desired viewController's index而不是 2,把你想要的 viewController 的索引
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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.