gpt4 book ai didi

ios - 同时使用导航 Controller 和标签栏

转载 作者:行者123 更新时间:2023-12-01 16:11:47 25 4
gpt4 key购买 nike

我使用导航 Controller 来推送我的 View 。像这样:

func showDetailForCategory(product: Category){

let storyBoard = UIStoryboard(name: "Main", bundle:nil)
let categoryViewController = storyBoard.instantiateViewController(withIdentifier: "categoryDetail") as! CategoryViewController

categoryViewController.categoryId = product.id
self.navigationController?.pushViewController(categoryViewController, animated:true)

}

它工作得很好。但我也想使用工具栏。我的 AppDelegate 如下所示

    let layout = UICollectionViewFlowLayout()
let featuredAppsController = FeaturedAppsController(collectionViewLayout: layout)
window?.rootViewController = UINavigationController(rootViewController: featuredAppsController)

let vc1 = featuredAppsController
vc1.tabBarItem.title = "Orange"
vc1.tabBarItem.image = UIImage(named: "heart")

// Set up the second View Controller
let vc2 = UIViewController()
vc2.view.backgroundColor = UIColor.purple
vc2.tabBarItem.title = "Purple"
vc2.tabBarItem.image = UIImage(named: "star")

// Set up the Tab Bar Controller to have two tabs
let tabBarController = UITabBarController()
tabBarController.viewControllers = [vc1, vc2]

// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()

我添加了 TabBarController。它工作正常。但是我的 NavigationController 不起作用。我认为这是因为我为其中两个设置了 rootViewController。我应该如何解决这个问题?感谢您的宝贵时间。

最佳答案

你需要这样的东西

let firstVc = UIViewController()
let secondVc = UIViewController()

let firstNav = UINavigationController(rootViewController: firstVc)
let secondNav = UINavigationController(rootViewController: secondVc)

let tabBarController = UITabBarController()
tabBarController.viewControllers = [firstNav, secondNav]

window?.rootViewController = tabBarController
window?.makeKeyAndVisible()

当然,填写你的类(class)等等,但基本思想是这样的

关于ios - 同时使用导航 Controller 和标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62704537/

25 4 0