创建标签栏控制器和导航控制器 [英] Create Tab bar controller and Navigation controller

查看:52
本文介绍了创建标签栏控制器和导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,但使用 XIB 文件,所以如果我在应用程序委托中添加此代码以创建标签栏控制器

I have an application but using XIB file so if I add this code in app delegate to create tab bar controller

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

和这段代码来创建导航控制器

and this code to create navigation controller

let viewController = SearchViewController(nibName: nil, bundle: nil)
    let navigationController = UINavigationController(rootViewController: viewController)

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

不能,因为我将 self.window?.rootViewController = navigationControllerwindow?.rootViewController = tabBarController 添加在一起.我想要的是这样的:

it can't because I add self.window?.rootViewController = navigationController and window?.rootViewController = tabBarController together. What I want is something like this:

但在代码中,我需要导航控制器来推送视图控制器.

but in code, I require navigation controller to push view controller.

推荐答案

didFinishLaunchingWithOptions下写如下代码:-

//创建标签控制器

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
//Create navigation controller
 let navigationController = UINavigationController(rootViewController: tabBarController)

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController//Set navigation controller as window's root view
    self.window?.makeKeyAndVisible()

这篇关于创建标签栏控制器和导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆