以编程方式在Swift中的选项卡之间切换

时间:2014-08-15 11:51:39

标签: ios xcode swift uitabbarcontroller viewcontroller

我需要编写一些代码,以便在iOS应用程序启动时将视图切换到另一个选项卡(例如,默认情况下显示第二个选项卡而不是第一个选项卡。)

我是Swift的新手,并制定了以下内容:

  • 代码应该放在第一个选项卡的ViewController的override func viewDidLoad()函数中。

  • 以下代码显示了第二个ViewController,但没有显示底部的标签栏(vcOptions是第二个ViewController标签项:

let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)

我认为答案可能在于使用UITabbarController.selectedIndex = 1,但不太确定如何实现它。

10 个答案:

答案 0 :(得分:93)

如果您的window rootViewControllerUITabbarController(大多数情况下是这样),那么您可以tabbar中的didFinishLaunchingWithOptions访问AppDelegate文件。

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    if let tabBarController = self.window!.rootViewController as? UITabBarController {
        tabBarController.selectedIndex = 1
    }

    return true
}

这将打开indexselectedIndex给定(1)的标签。

如果您在viewDidLoad firstViewController中执行此操作,则需要按标记或其他方式进行管理以跟踪所选标签。在didFinishLaunchingWithOptions文件或AppDelegate自定义类rootViewController的{​​{1}}中执行此操作的最佳位置。

答案 1 :(得分:28)

1.创建一个超级UITabBarController的新类。 E.g:

self.selectedIndex = 1; //set the tab index you want to show here, start from 0

2.将以下代码添加到函数viewDidLoad():

jobs:
 - name: A
   schedule: "0 0/5 * 1/1 * ? *"
   type: mongodb.cluster
   config:
     host: mongodb://localhost:27017/admin?replicaSet=rs
     minSecondaries: 2
     minOplogHours: 100
     maxSecondaryDelay: 120
 - name: B
   schedule: "0 0/5 * 1/1 * ? *"
   type: mongodb.cluster
   config:
     host: mongodb://localhost:27017/admin?replicaSet=rs
     minSecondaries: 2
     minOplogHours: 100
     maxSecondaryDelay: 120

3.转到storyboard,将Tab Bar Controller的Custom Class设置为这个新类。 (MyVotes1作为图片中的例子)

enter image description here

答案 2 :(得分:22)

Swift 3

您可以将此代码添加到tabBarController中的默认视图控制器(index 0):

    override func viewWillAppear(_ animated: Bool) {
        _ = self.tabBarController?.selectedIndex = 1
    }

加载后,这会自动将标签移动到列表中的第二项,但也允许用户随时手动返回该视图。

答案 3 :(得分:18)

要扩展@ codester的答案,您无需检查然后分配,您可以一步完成:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    if let tabBarController = self.window!.rootViewController as? UITabBarController {
        tabBarController.selectedIndex = 1
    }

    return true
}

答案 4 :(得分:13)

viewController必须是 UITabBarControllerDelegate 的孩子。因此,您只需在 SWIFT 3

上添加以下代码即可
self.tabBarController?.selectedIndex = 1

答案 5 :(得分:3)

雨燕5

//MARK:- if you are in UITabBarController 
self.selectedIndex = 1

tabBarController?.selectedIndex = 1

答案 6 :(得分:2)

在典型的应用程序中,有一个UITabBarController并将其嵌入3个或更多UIViewController作为其选项卡。在这种情况下,如果将UITabBarController子类化为YourTabBarController,则可以通过以下方式简单地设置选定的索引:

selectedIndex = 1 // Displays 2nd tab. The index starts from 0.

如果要从其他任何视图导航到YourTabBarController,则可以在该视图控制器的prepare(for segue :)方法中进行以下操作:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if segue.identifier == "SegueToYourTabBarController" {
            if let destVC = segue.destination as? YourTabBarController {
                destVC.selectedIndex = 0
            }
        }

我正在通过Xcode 10和Swift 4.2设置标签的方式。

答案 7 :(得分:0)

仅需更新,在iOS 13之后,我们现在有了SceneDelegates。因此,可以选择将所需的选项卡选择放在SceneDelegate.swift中,如下所示:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, 
               willConnectTo session: UISceneSession, 
               options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        if let tabBarController = self.window!.rootViewController as? UITabBarController {
            tabBarController.selectedIndex = 1
        }

    }

答案 8 :(得分:0)

如果您想从作为特定视图控制器的一部分编写的代码中执行此操作,例如响应按钮按下或其他操作,您可以这样做:

@IBAction func pushSearchButton(_ sender: UIButton?) {
    if let tabBarController = self.navigationController?.tabBarController  {
        tabBarController.selectedIndex = 1
    }
}

您还可以添加代码来使用 UITabBarControllerDelegate 方法处理选项卡切换。在每个选项卡的基本视图控制器上使用标签,您可以看到自己的位置并采取相应的行动:例如

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    
    // if we didn't change tabs, don't do anything
    if tabBarController.selectedViewController?.tabBarItem.tag ==  viewController.tabBarItem.tag {
        return false
    }
    
    if viewController.tabBarItem.tag == 4096 { // some particular tab
        // do stuff appropriate for a transition to this particular tab
    }
    else if viewController.tabBarItem.tag == 2048 { // some other tab
        // do stuff appropriate for a transition to this other tab
    }
}

答案 9 :(得分:0)

Swift 5

如果你的类是 UITabBarController 的子类,你可以这样做:

selectedViewController = viewControllers![yourIndex]