简体   繁体   English

如何使用 StoryBoard 在 UITabBarController 中设置选定的选项卡?

[英]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.

相关问题 如何为每个标签使用使用相同ViewController的UITabBarController? - How do I use a UITabBarController using the same ViewController for every tab? 如何以编程方式设置UITabBarController的选定选项卡,同时还触发UITabBarControllerDelegate中的shouldSelectViewController - How can I programmatically set selected tab of UITabBarController while also triggering shouldSelectViewController in UITabBarControllerDelegate Xcode故事板选项卡式应用程序(UITabBarController)-告知何时选择选项卡 - Xcode Storyboard Tabbed application (UITabBarController) - tell when tab selected 如何使用Storyboard重新排列UITabBarController项目? - How can I rearrange UITabBarController items using the Storyboard? 如何从 Storyboard 将 UITabBarController 设置为 rootViewController - How to set a UITabBarController as rootViewController from Storyboard 在UITabBarController上选择选项卡时,如何显示ViewController? - How to present ViewController when tab is selected on UITabBarController? UITabBarController选择的选项卡错误(?) - UITabBarController selected tab bug (?) 如何将单独的故事板链接到uitabbarcontroller中的每个特定选项卡? - How to link a separate storyboard to each particular tab in uitabbarcontroller? 如何为UITabBarController中包含两次的同一情节提要板设置不同的UITabBarItems? - How can I set different UITabBarItems for the same storyboard reference included twice in a UITabBarController? 在UITabBarController中设置选定的图像 - Set selected image in UITabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM