swift - UINavigationController、UITabBarController、UITableViewController

标签 swift uitableview uinavigationcontroller uitabbarcontroller appdelegate

我目前已将应用程序设置为 UINavigationController 作为初始 View ,其中 UITableViewController 作为其 Root View Controller 。到目前为止,该应用程序运行良好。我在 AppDelegate.swift 中有以下代码:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let favLibrary = FavLibrary()

    let navController = window!.rootViewController as! UINavigationController
    let favController = navController.topViewController as! FLViewController
    favController.favLibrary = favLibrary

    return true
}

我正在尝试实现一个 UITabBarController,以便我可以使用选项卡栏在同一级别(收藏夹和库)的两个 UITableViewController 之间切换。

我将每个 VC 嵌入到其自己的导航 Controller 中,然后将两个导航 Controller 嵌入到一个选项卡栏 Controller 中。

运行应用程序时,它崩溃并出现以下错误:

无法将“UITabBarController”(0x115f9e430) 类型的值转换为“UINavigationController”(0x115f971d0)。 2018-09-27 15:49:43.811377-0700 appName [3675:954448] 无法将类型“UITabBarController”(0x115f9e430)的值转换为“UINavigationController”(0x115f971d0)。

如何更正 AppDelegate 中的代码以保留选项卡栏和导航 Controller 的新排列的功能?

最佳答案

如果我正确理解你的问题,我想你应该首先访问 tabBarController,然后检索它包含的 View Controller ,它应该是导航 Controller 的列表。然后您可以将 View Controller 放入所选的导航 Controller 中:

let tabBar = window!.rootViewController as! UITabBarController
let targetTabNav = tabBar.viewControllers![1] as! UINavigationController // change index to what you want
let targetVc = targetTabNav.viewControllers.first!
// Do what you want with the target Vc ...

关于swift - UINavigationController、UITabBarController、UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546139/

相关文章:

Swift try catch 错误

ios - 如何跟踪随日期变化的数据?

ios - UITableview 标题栏带有星期日期?

ios - Swift PrepareForSegue NSIndexPath 错误

swift - 在 tableView 中选择后在 detailView 中显示来自 URL 的图像

ios - 防止自定义容器中的 UINavigationBar 动画

ios - 如何在 Swift 中使用 SlideNavigationController?

ios - 从嵌入在 tableView 中的 collectionView 显示 xib

swift - 如何从 Firebase 中检索手动输入的数据?

ios - childByAppendingPath 由于嵌入导航 Controller 而导致 Segues