繁体   English   中英

使用tabBar从应用程序中的navBar推送视图

[英]Push view from navBar in app with tabBar

我试图制作一个既有UITabBar (作为根视图控制器)又有UINavigationBar的应用程序,这些应用程序在整个应用程序中始终存在。 我的问题是我试图从navBar推送一个viewcontroller,但是我不知道该怎么做。 我知道我需要UINavigationBar成为我要从中推送的视图控制器的NavigationController ,但是我不知道该怎么做。

在appDelegate中:

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

    let tabBar = window!.rootViewController
    let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: window!.frame.width, height: 64))

    let navBarItem = UINavigationItem(title: "Title")
    navBar.items = [navBarItem]

    tabBar?.view.addSubview(navBar)

    return true

}

在ViewController中,我试图从中推送新的VC

override func viewDidLoad() {
    super.viewDidLoad()

    let navBar = self.parentViewController?.view.subviews[2] as! UINavigationBar
    let navBarItem = navBar.items[0] as! UINavigationItem

    navBarItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "pushCreateVC:")
}

func pushCreateVC(sender: UIBarButtonItem) {

    println("push")

    let navBar = self.parentViewController?.view.subviews[2] as! UINavigationBar
    let navBarItem = navBar.items[0] as! UINavigationItem //left over code to reference the navBar from this method

    let createVC = CreateViewController()
    self.navigationController?.pushViewController(createVC, animated: true) // This is where I want the navBar to push createVC so it still have the navBar at the top with a back button

}

如果您想做在问题中提到的事情,则应使用UINavgationController创建一个UITabbarController

//        First you need to create a nav controller with viewcontroller in it

var vc = UIViewController()
var navController = UINavigationController(rootViewController: vc)

var tbController = UITabBarController()

//you can add more tab with more navControllers or viewcontrollers
tbController.setViewControllers([navController], animated: true)

//present the tbController as the main viewcontroller

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM