在 swift 中点击 tabBar 时如何显示不同的 ViewController
How to show different ViewControllers when tapping on tabBar in swift
我有三个 viewController,其中两个连接到 tabBarController。因此,当点击 tabBar 项 here is my storyboard
时,应用程序应该显示两个 vc 的依赖布尔值之一
import UIKit
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let userLoggedIn: Bool!
if tabBarController.selectedIndex == 1{
if userLoggedIn == true{
// show firstVC
}else{
// show secondVC
}
}
}
}
您可以使用子ViewController,父ViewController来自Storyboard。
override func viewDidLoad() {
super.viewDidLoad()
///add two ChildVC here, hide 1 ifneeded
}
单击此选项卡时,您会选中 userLoggedIn
。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if userLoggedIn == true {
/// show first childVC
/// hide second childVC
} else {
/// hide first childVC
/// show second childVC
}
}
您可以查看演示:Demo
只需在修改userLoggedIn变量时设置viewController列表即可(假设userLoggedIn为TabBarCOntroller实例变量):
class TabBarC: UITabBarController {
@IBOutlet var firstVC : FirstViewController!
@IBOutlet var secondVC : SecondViewController!
@IBOutlet var thirdVC : ThirdViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
for vc in self.viewControllers! {
if let fv = vc as? FirstViewController {
firstVC = fv
} else if let fv = vc as? SecondViewController {
secondVC = fv
} else if let fv = vc as? ThirdViewController {
thirdVC = fv
}
}
}
var userLoggedIn : Bool = false {
didSet {
if userLoggedIn {
self.viewControllers = [firstVC, thirdVC]
} else {
self.viewControllers = [secondVC, thirdVC]
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self. userLoggedIn = false
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 3 { // For purpose of test i set tag for 3 VC to 1 for firsttVC, 2 for secondVC, 3 for thirdVC
// for test : change login value each time thirdVC is selected
userLoggedIn = ! userLoggedIn
}
}
因此,每次在代码中的任何位置设置 userLoggedIn 时,选项卡都会显示所需的选项卡栏项目。
3 VC 被添加到 stpryboard 中的标签栏。
选择第三VC时,第一个选项卡栏项目在一个和二
之间更改
只需像这样在 viewDidLoad 中将 .overCurrentContext 添加到 modalPresentationStyle。
let newVC = self.storyboard?.instantiateViewController(withIdentifier:"secondVC")
newVC?.modalPresentationStyle = .overCurrentContext
self.present(newVC!, animated: false, completion: nil)
我有三个 viewController,其中两个连接到 tabBarController。因此,当点击 tabBar 项 here is my storyboard
时,应用程序应该显示两个 vc 的依赖布尔值之一import UIKit
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let userLoggedIn: Bool!
if tabBarController.selectedIndex == 1{
if userLoggedIn == true{
// show firstVC
}else{
// show secondVC
}
}
}
}
您可以使用子ViewController,父ViewController来自Storyboard。
override func viewDidLoad() {
super.viewDidLoad()
///add two ChildVC here, hide 1 ifneeded
}
单击此选项卡时,您会选中 userLoggedIn
。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if userLoggedIn == true {
/// show first childVC
/// hide second childVC
} else {
/// hide first childVC
/// show second childVC
}
}
您可以查看演示:Demo
只需在修改userLoggedIn变量时设置viewController列表即可(假设userLoggedIn为TabBarCOntroller实例变量):
class TabBarC: UITabBarController {
@IBOutlet var firstVC : FirstViewController!
@IBOutlet var secondVC : SecondViewController!
@IBOutlet var thirdVC : ThirdViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
for vc in self.viewControllers! {
if let fv = vc as? FirstViewController {
firstVC = fv
} else if let fv = vc as? SecondViewController {
secondVC = fv
} else if let fv = vc as? ThirdViewController {
thirdVC = fv
}
}
}
var userLoggedIn : Bool = false {
didSet {
if userLoggedIn {
self.viewControllers = [firstVC, thirdVC]
} else {
self.viewControllers = [secondVC, thirdVC]
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self. userLoggedIn = false
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 3 { // For purpose of test i set tag for 3 VC to 1 for firsttVC, 2 for secondVC, 3 for thirdVC
// for test : change login value each time thirdVC is selected
userLoggedIn = ! userLoggedIn
}
}
因此,每次在代码中的任何位置设置 userLoggedIn 时,选项卡都会显示所需的选项卡栏项目。 3 VC 被添加到 stpryboard 中的标签栏。 选择第三VC时,第一个选项卡栏项目在一个和二
之间更改只需像这样在 viewDidLoad 中将 .overCurrentContext 添加到 modalPresentationStyle。
let newVC = self.storyboard?.instantiateViewController(withIdentifier:"secondVC")
newVC?.modalPresentationStyle = .overCurrentContext
self.present(newVC!, animated: false, completion: nil)