更改选项卡栏中选项卡的目标视图控制器

Change the destination View Controller of a tab in a Tab Bar

我真的很想知道如何更改 swift 中标签栏项目的 link/destination ViewController。

我的情况:

if (beforeDayX)
{
    //TabBar[0] (Home) shows my1Scene with my1ViewController 
} else
{
    // TabBar[0] (Home) shows my2Scene with my2ViewController 
}

我应该怎么做?可以吗?

注意:最初的答案是以编程方式创建 tabBar。但是由于 Josep(提问者)已经通过 Storyboard 创建了 tabBar,因此我在这里更改答案以满足需要。

假设:

(1) TabBar 是使用 UITabBarViewController 创建的。 Class 名称:TabBarViewController

(2)最初,Tabbar由WeekendVC、WeekdayVC、OtherVC组成。

(3) 根据条件,Tabbar 将变为:(WeekendVC 和 OtherVC)或(WeekdayVC 和 OtherVC)。

TabBarViewController 如下所示:

import UIKit

enum TypeOfDay {
    case weekday
    case weekend
}

class TabBarViewController: UITabBarController {

    var typeOfDay: TypeOfDay = .weekday

    override func viewDidLoad() {
        super.viewDidLoad()

        // Initial order is: WeekendVC, WeekdayVC, OtherVC
        if let currentViewControllers = self.viewControllers {

            let weekendVC = currentViewControllers[0]
            let weekdayVC = currentViewControllers[1]
            let otherVC = currentViewControllers[2]

            switch typeOfDay {
            case .weekend:
                self.viewControllers = [weekendVC, otherVC]
            case .weekday:
                self.viewControllers = [weekdayVC, otherVC]
            }
        }
    }
}