gpt4 book ai didi

swift - 从 Tabbar 更改 ViewController

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:46 25 4
gpt4 key购买 nike

我有一个带有 3 个选项卡的 UITabBarController 和一个 SettingsVC,您可以在其中选择 MainVC 的外观(有 2 个不同的 ViewControllers 基于用户的偏好应该显示为 MainVC)。

我制作了应用程序并且它可以工作,但它确实有问题,因为我只是将 ViewControllers 推到彼此之上,在某些特定情况下,您会在切换 tabs< 时看到错误.

Main.storyboard

enter image description here

这是我的代码的一部分(在尝试了很多东西之后),它根据 UserDefaults

更改了 VC

CheckSettingsStatus.swift

//
// Check for Double Column Settings and navigate to Double Column VC
//
func checkMainVCViewStatusAndNavigateAcordingly(){
if UserDefaults.standard.integer(forKey: KEY_MAIN_VC_VIEW) == 1{
//let appDel = UIApplication.shared.delegate as! AppDelegate
//appDel.goToDoubleMainVC()

let mainVC = storyboard?.instantiateViewController(withIdentifier: "DoubleColumnMainVC") as! DoubleColumnMainVC
self.navigationController?.viewControllers[0] = mainVC
//navigationController?.pushViewController(mainVC, animated: false)

var navigationArray = self.navigationController?.viewControllers
print("Number of VCs = \(String(describing: navigationArray?.count))")
if navigationArray?.count == nil{
self.navigationController?.pushViewController(mainVC, animated: false)
}else{
self.navigationController?.popToRootViewController(animated: false)
}
// self.navigationController?.viewControllers = navigationArray!
}
}


//
// same as function above but goes to MainVC
//
func checkDoubleColumnMainVCViewStatusAndNavigateAcordingly(){
if UserDefaults.standard.integer(forKey: KEY_MAIN_VC_VIEW) == 0{
// let appDel = UIApplication.shared.delegate as! AppDelegate
// appDel.goToMainVC()
let mainVC = storyboard?.instantiateViewController(withIdentifier: "MainVC") as! MainVC
self.navigationController?.viewControllers[0] = mainVC

var navigationArray = self.navigationController?.viewControllers
print("Number of VCs = \(String(describing: navigationArray?.count))")
if navigationArray?.count == nil{
navigationController?.pushViewController(mainVC, animated: false)
}else{
self.navigationController?.popToRootViewController(animated: false)
}
// self.navigationController?.viewControllers = navigationArray!
}
}

在这两个 Controller 的 ViewWillAppear 中,我调用这些函数进行相应的切换。

知道我做错了什么吗?我可以从 UITabBarController 而不是 NavigationController 推送 Controller 吗?像这样的东西:

tabBarController.pushViewController(mainVC, animated: false)

代替:

navigationController.pushViewController(mainVC, animated: false)

提前致谢

更新:

在我最好的情况下,当一切正常时,在第一次启动应用程序时(到第二个 mainVC),NavBar 中的 Buttons 没有'工作。

编辑:

我刚刚意识到我在 SecondMainVC 上方看到的 NavigationBar 是来自 MainVCNavigationBar ,这就是按钮不起作用的原因。这怎么可能?

最佳答案

我在尝试在 Storyboard中开发标签栏 Controller 时遇到了同样的问题,因此我发现最好的方法是通过代码实现它。所以你可以让你的代码看起来像这样:

1) 创建一个UITabController的子类,并让它符合UITabBarControllerDelegate

  class TabBarVC: UITabBarController , UITabBarControllerDelegate {

2)然后在你的类中编写这个方法来初始化你项目中的选项卡:

  private func settingUpTabBarAndInitializingViewControllers(){

//self delagting
self.delegate = self

//home tab
let homeStoryboard = UIStoryboard(name: Constants.StoryBoards.homeStoryboard, bundle: nil)
let homeVC = homeStoryboard.instantiateViewController(withIdentifier: Constants.ControllersIDs.homeScreen) as! HomeVC

// shop tab
let shopStoybaord = UIStoryboard(name: Constants.StoryBoards.shopStoryboard, bundle: nil)
let shopVC = shopStoybaord.instantiateViewController(withIdentifier: Constants.ControllersIDs.shopScreen) as! ShopVC

//offers tab
let offersStoryboard = UIStoryboard(name: Constants.StoryBoards.offersStoryboard, bundle: nil)
let offersVC = offersStoryboard.instantiateViewController(withIdentifier: Constants.ControllersIDs.offersScreen) as! OffersVC

//more tab
let moreStoryboard = UIStoryboard(name: Constants.StoryBoards.MoreScreenStoryboard, bundle: nil)
let moreVC = moreStoryboard.instantiateViewController(withIdentifier: Constants.ControllersIDs.moreVCScreen) as! MoreOptionsTVC


//setting VCs
self.viewControllers = [homeVC , shopVC , offersVC , moreVC]


//setting buttons

//buttons images
let homeImage = UIImage(named: "TabHome")
let shopImage = UIImage(named : "TabShop")
let offerImage = UIImage(named: "TabOffers")
let moreImage = UIImage(named: "TabMenu")


// buttons shakhsiyan
let homeButton = UITabBarItem(title: homeText, image: homeImage, selectedImage: homeTappedImage)
let shopButton = UITabBarItem(title: shopText, image: shopImage, selectedImage: shopTappedImage)
let offersButton = UITabBarItem(title: offersText, image: offerImage, selectedImage: offersTappedImage)
let moreButton = UITabBarItem(title: moreText, image: moreImage, selectedImage: moreTappedImage)


homeVC.tabBarItem = homeButton
shopVC.tabBarItem = shopButton
offersVC.tabBarItem = offersButton
moreVC.tabBarItem = moreButton

}

附言您可以根据需要添加尽可能多的选项卡,并且在其中滚动时您不会遇到任何错误,

确保在标签栏 Controller 之前只有一个导航 Controller 。

关于swift - 从 Tabbar 更改 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com