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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:00:16  来源:igfitidea点击:

Setting the default tab when using storyboards

iosuitabbarcontrollerstoryboarduitabbar

提问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:

虽然您可以像其他答案一样以编程方式设置初始选定的选项卡,但要在故事板中实现相同的效果而无需接触代码,您将执行以下操作:

  1. Select the Tab Bar Controller in the Storyboard Interface
  2. Show the Identity Inspector in the Utilities panel
  3. Add a new "User Defined Runtime Attribute"
  4. Set the Key Path to "selectedIndex"
  5. Set the Type to "Number"
  6. Set the Value to the index of the tab you wish to select (a value of 1 would select the second tab for example)
  7. Save the Storyboard, build and run the application
  1. 在 Storyboard 界面中选择 Tab Bar Controller
  2. 在“实用工具”面板中显示身份检查器
  3. 添加新的“用户定义的运行时属性”
  4. 将键路径设置为“selectedIndex”
  5. 将类型设置为“数字”
  6. 将值设置为您要选择的选项卡的索引(例如,值为 1 将选择第二个选项卡)
  7. 保存 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,但是,我认为它提供了最干净的解决方案。

  1. Create BaseTabBarController.swift
  2. Add an @IBInspectableand set it in viewDidLoad:

    class BaseTabBarController: UITabBarController {
    
        @IBInspectable var defaultIndex: Int = 0
    
        override func viewDidLoad() {
            super.viewDidLoad()
            selectedIndex = defaultIndex
        }
    
    }
    
  3. In the storyboard, set you UITabBarControllerto be your new subclass:

  1. 创建 BaseTabBarController.swift
  2. 添加一个@IBInspectable并将其设置为viewDidLoad

    class BaseTabBarController: UITabBarController {
    
        @IBInspectable var defaultIndex: Int = 0
    
        override func viewDidLoad() {
            super.viewDidLoad()
            selectedIndex = defaultIndex
        }
    
    }
    
  3. 在故事板中,将您UITabBarController设置为您的新子类:

enter image description here

在此处输入图片说明

  1. Go to the Attributes Inspectoradd set the new property Default Index:
  1. 转到Attributes Inspector添加设置新属性Default Index

enter image description here

在此处输入图片说明

  1. ta-da! (:
  1. 达达!(:

回答by DevonDahon

  1. Create a new file subclass of UITabBarController;
  2. Add this at the end of viewDidLoad:

    self.selectedIndex = 1;

  3. Set this new file as the Custom Class in the UITabBarControllerof your Storyboard.

  1. 创建一个新的文件子类UITabBarController
  2. 在末尾添加viewDidLoad

    self.selectedIndex = 1;

  3. 将此新文件设置为UITabBarControllerStoryboard 中的自定义类。

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 applicationDidBecomeActivefunction 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 selectedIndexto whatever you want. (0would be the first, 3would 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 所说的步骤,但是:

  1. Select the bar item instead of the TabBar
  2. Got to "User Defined Runtime Attribute"
  3. Add new key
  4. Name it "selected"
  5. Set its type to boolean and choose true / or check the checkbox
  1. 选择栏项目而不是 TabBar
  2. 转到“用户定义的运行时属性”
  3. 添加新密钥
  4. 将其命名为“选定”
  5. 将其类型设置为 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]
        }
    }
}