gpt4 book ai didi

ios - 我想通过在 Swift 中以编程方式将 UINavigationController 与 UITableViewController 嵌入,这是 UITabBarViewController 的 subview

转载 作者:行者123 更新时间:2023-11-28 09:17:18 25 4
gpt4 key购买 nike

最近想做一个自学的App,这个App是iOS的通讯录App。首先,我有两个 UITableViewControllers 连接到 UITabBarViewController。然后我想通过编码将 UINavigationController 与这两个 TableView 一起嵌入(在这种情况下,我不想通过设计将它嵌入 Storyboard 中)。但我坚持下去。这是我在 AppDelegate

中的代码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let tabBar : UITabBarViewController = UITabBarViewController()
let tableView1 : UITableViewController = UITableViewController()
let nav = UINavigationController(rootViewController: tableView1)
let tableView2 : UITableViewController = UITableViewController()
let nav2 = UINavigationController(rootViewController: tableView2)
tabBar.viewControllers = [nav, nav2]
self.window?.rootViewController = tabBar

return true
}

并且结果总是显示第一个tableView1。我怎样才能在 UITabBarViewController 中显示两个选项卡选项(在 View 的底部)?如果我通过在 Storyboard 中进行设计来实现,我相信它会很好地工作。但是通过编码,我不知道如何修复它。请指导我。提前致谢!

最佳答案

UIKit 中没有UITabBarViewController。它称为 UITabBarController。您的 ViewControllers 需要有 NavigationItems。如果您像在代码中那样显示它们,您将在底部有一个 Tabbar,但上面没有任何可见内容。在您的情况下,NavigationControllers 是您需要定义 nav.navigationItem.title = ...

编辑:我意识到你没有初始化窗口。这与您的其他问题无关,但您的代码看不到任何内容。所以你必须把它改成:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
self.window?.rootViewController = tabBar
self.window?.makeKeyAndVisible();

关于ios - 我想通过在 Swift 中以编程方式将 UINavigationController 与 UITableViewController 嵌入,这是 UITabBarViewController 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942245/

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