我正在尝试创建一个同时包含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中尝试从
推送新VCoverride 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
}
答案 0 :(得分:0)
如果你想做你在问题中提到的内容,你应该在其中创建一个带有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