如何以编程方式添加导航控制器,我使用XIB文件swift

时间:2017-01-19 11:58:55

标签: swift xib

我想创建导航栏但没有任何反应

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let viewController = DummyViewController(nibName: nil, bundle: nil) 
    let navigationController = UINavigationController(rootViewController: viewController)

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    let tabBarController = UITabBarController()
    let tabViewController1 = DummyViewController(
        nibName: "DummyViewController",
        bundle: nil)
    let tabViewController2 = SearchViewController(
        nibName:"SearchViewController",
        bundle: nil)

    tabViewController1.tabBarItem = UITabBarItem(
        title: "Location",
        image: UIImage(named: "ic_location_blue"),
        tag: 1)
    tabViewController2.tabBarItem = UITabBarItem(
        title: "Search",
        image:UIImage(named: "ic_search_blue") ,
        tag:2)


    let controllers = [tabViewController1,tabViewController2]
    tabBarController.viewControllers = controllers
    window?.rootViewController = tabBarController

    return true
}

似乎是因为我添加了self.window?.rootViewController = navigationControllerwindow?.rootViewController = tabBarController

我正在使用xib文件,并且不知道如何将标签栏控制器与导航控制器结合使用,谢谢之前。

3 个答案:

答案 0 :(得分:2)

你可以试试这个

let viewController = DummyViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController()
navigationController.viewControllers = [viewController!]
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()

希望它有所帮助。

答案 1 :(得分:1)

使用

解决
let FirstViewController: HomeViewController = HomeViewController(nibName: "HomeViewController", bundle: nil)
    let navigationBarFirst = UINavigationController(rootViewController: FirstViewController)

    let SecondViewController: SearchViewController = SearchViewController(nibName: "SearchViewController", bundle: nil)
    let navigationBarSecond = UINavigationController(rootViewController: SecondViewController)

    let ThirdViewController: InformationViewController = InformationViewController(nibName: "InformationViewController", bundle: nil)
    let navigationBarThird = UINavigationController(rootViewController: ThirdViewController)

    let FourthViewController: FavoritesViewController = FavoritesViewController(nibName: "FavoritesViewController", bundle: nil)
    let navigationBarFourth = UINavigationController(rootViewController: FourthViewController)

    let FifthViewController: MenuViewController = MenuViewController(nibName: "MenuViewController", bundle: nil)
    let navigationBarFifth = UINavigationController(rootViewController: FifthViewController)

    let tabBarController = UITabBarController()

    let controllers = [navigationBarFirst, navigationBarSecond, navigationBarThird, navigationBarFourth, navigationBarFifth]

答案 2 :(得分:0)

您正在将nil写为笔尖名称。输入笔尖名称,如下所示: -

let viewController = DummyViewController(nibName: "DummyViewController", bundle: nil)