[英]Change the destination View Controller of a tab in a Tab Bar
我真的很想知道如何快速更改选项卡栏项的链接/目标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创建的。 类名:TabBarViewController
(2)最初,选项卡栏由WeekendVC,WeekdayVC,OtherVC组成。
(3)根据条件,选项卡栏将变为:(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]
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.