如何在 Tab Bar Controller 上使用高级导航
How to use advanced navigation on Tab Bar Controller
我有一个 TabBarController
有 5 个选项。第一个 TabBarItem 是 HomeScreen,第二个是带我进入深度导航工作流。当我在该深度导航的最后一个 ViewController
上时,我有一个 Save
按钮,当我单击它时,我想将用户送回主屏幕。问题是 TabBarController
我的所有图标都消失了。我尝试了另一种解决方案,但对于该解决方案,我的图标显示了,但是当我单击第二个 TabBarItem
时,显示的是深度导航工作流程的最后一个屏幕。
这是我的项目:https://github.com/AdvancedNavigationTabBarController
这是我的代码:
第一个标签栏项目
class HomeVC: UIViewController {
@IBOutlet var textfieldHoldingScootersValue: UITextField!
@IBOutlet var textfieldHoldingBicyclesValue: UITextField!
var valueForScootersScreen = String()
var valueForBicyclesScreen = String()
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Selected index: \(self.tabBarController?.selectedIndex ?? -1)")
}
override func viewWillDisappear(_ animated: Bool) {
valueForScootersScreen = textfieldHoldingScootersValue.text ?? String()
valueForBicyclesScreen = textfieldHoldingBicyclesValue.text ?? String()
if let navController = self.tabBarController?.viewControllers?[1] as? UINavigationController{
if let scootersTab = navController.children.first as? BuyNewScooterOrBikeVC{
scootersTab.receivedValueFromHomeScreen = valueForScootersScreen
}
}
if let navController = self.tabBarController?.viewControllers?[2] as? UINavigationController{
if let bicyclesTab = navController.children.first as? BuyNewScooterOrBikeVC{
bicyclesTab.receivedValueFromHomeScreen = valueForBicyclesScreen
}
}
}
第二个标签栏项目
class BuyNewScooterOrBikeVC: UIViewController {
@IBOutlet var receivedValueLabel: UILabel!
var receivedValueFromHomeScreen = String()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("Selected index: \(self.tabBarController?.selectedIndex ?? -1)")
if self.tabBarController?.selectedIndex == 1 {
receivedValueLabel.text = "Value received for Scooters: \n" + receivedValueFromHomeScreen
self.navigationItem.title = "Buy a Scooter"
}
if self.tabBarController?.selectedIndex == 2 {
receivedValueLabel.text = "Value received for Bicycles: \n" + receivedValueFromHomeScreen
self.navigationItem.title = "Buy a Bicycle"
}
}
}
第二个标签栏项目的最后一个视图控制器
class ScooterOrBikeLastVC: UIViewController {
@IBAction func saveBtnTapped(_ sender: UIButton) {
// Navigate to Home Screen
// self.parent?.navigationController?.popToRootViewController(animated: true) // if I use this line then when I click on the same Tab Bar is sending me to the last screen and I want to be on the first screen of that navigation workflow
self.navigationController?.popToRootViewController(animated: true) // If I use this line when I click Save btn I don't see anymore the icons on Tab Bar Controller
self.tabBarController?.selectedIndex = 0
}
}
感谢阅读本文!
在弹出到 root 之前将 tabIndex 设置为 0。
self.tabBarController?.selectedIndex = 0
self.navigationController?.popToRootViewController(animated: true)
// class ScooterOrBikeLastVC
// self.parent?.navigationController?.popToRootViewController(animated: true) // 这行什么都不做,因为父级是 nil
那你为什么会得到“主页”选项卡?
因为您已将标签栏设置为 0
self.tabBarController?.selectedIndex = 0
当您希望编写产品代码时,您应该想出更好的方法来在控制器之间进行通信。在您当前的方法中,任何控制器都可以访问和修改另一个控制器。
我有一个 TabBarController
有 5 个选项。第一个 TabBarItem 是 HomeScreen,第二个是带我进入深度导航工作流。当我在该深度导航的最后一个 ViewController
上时,我有一个 Save
按钮,当我单击它时,我想将用户送回主屏幕。问题是 TabBarController
我的所有图标都消失了。我尝试了另一种解决方案,但对于该解决方案,我的图标显示了,但是当我单击第二个 TabBarItem
时,显示的是深度导航工作流程的最后一个屏幕。
这是我的项目:https://github.com/AdvancedNavigationTabBarController
这是我的代码:
第一个标签栏项目
class HomeVC: UIViewController {
@IBOutlet var textfieldHoldingScootersValue: UITextField!
@IBOutlet var textfieldHoldingBicyclesValue: UITextField!
var valueForScootersScreen = String()
var valueForBicyclesScreen = String()
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Selected index: \(self.tabBarController?.selectedIndex ?? -1)")
}
override func viewWillDisappear(_ animated: Bool) {
valueForScootersScreen = textfieldHoldingScootersValue.text ?? String()
valueForBicyclesScreen = textfieldHoldingBicyclesValue.text ?? String()
if let navController = self.tabBarController?.viewControllers?[1] as? UINavigationController{
if let scootersTab = navController.children.first as? BuyNewScooterOrBikeVC{
scootersTab.receivedValueFromHomeScreen = valueForScootersScreen
}
}
if let navController = self.tabBarController?.viewControllers?[2] as? UINavigationController{
if let bicyclesTab = navController.children.first as? BuyNewScooterOrBikeVC{
bicyclesTab.receivedValueFromHomeScreen = valueForBicyclesScreen
}
}
}
第二个标签栏项目
class BuyNewScooterOrBikeVC: UIViewController {
@IBOutlet var receivedValueLabel: UILabel!
var receivedValueFromHomeScreen = String()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("Selected index: \(self.tabBarController?.selectedIndex ?? -1)")
if self.tabBarController?.selectedIndex == 1 {
receivedValueLabel.text = "Value received for Scooters: \n" + receivedValueFromHomeScreen
self.navigationItem.title = "Buy a Scooter"
}
if self.tabBarController?.selectedIndex == 2 {
receivedValueLabel.text = "Value received for Bicycles: \n" + receivedValueFromHomeScreen
self.navigationItem.title = "Buy a Bicycle"
}
}
}
第二个标签栏项目的最后一个视图控制器
class ScooterOrBikeLastVC: UIViewController {
@IBAction func saveBtnTapped(_ sender: UIButton) {
// Navigate to Home Screen
// self.parent?.navigationController?.popToRootViewController(animated: true) // if I use this line then when I click on the same Tab Bar is sending me to the last screen and I want to be on the first screen of that navigation workflow
self.navigationController?.popToRootViewController(animated: true) // If I use this line when I click Save btn I don't see anymore the icons on Tab Bar Controller
self.tabBarController?.selectedIndex = 0
}
}
感谢阅读本文!
在弹出到 root 之前将 tabIndex 设置为 0。
self.tabBarController?.selectedIndex = 0
self.navigationController?.popToRootViewController(animated: true)
// class ScooterOrBikeLastVC // self.parent?.navigationController?.popToRootViewController(animated: true) // 这行什么都不做,因为父级是 nil 那你为什么会得到“主页”选项卡? 因为您已将标签栏设置为 0 self.tabBarController?.selectedIndex = 0
当您希望编写产品代码时,您应该想出更好的方法来在控制器之间进行通信。在您当前的方法中,任何控制器都可以访问和修改另一个控制器。