我是以编程方式呈现视图控制器,但是当出现视图控制器时,它缺少导航栏。在调用它时,有没有办法在这个新的视图控制器上以编程方式设置导航栏?
到目前为止,这是我的代码:
//LOAD CRICKET GAME
if gameGAMETYPE[index] == "Cricket" && gameGAMEPLAYERS[index] == "1" {
println("LOAD ONE PLAYER CRICKET")
//load OnePlayerCricket.swift
//replace tableData with TABLEDATA
//programatically present new view controller
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("OnePlayerCricketVC") as? OnePlayerCricket {
presentViewController(resultController, animated: true, completion: nil)
//programmatically present a navigation bar
NEED HELP HERE!!!! thank you :)
}
答案 0 :(得分:7)
以下是从firstView启动导航控制器的代码:
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("OnePlayerCricketVC") as? OnePlayerCricket {
let navController = UINavigationController(rootViewController: resultController) // Creating a navigation controller with resultController at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)
}
修改强>
如果您想在该导航中添加后退按钮,请将此代码用于OnePlayerCricket.swift
类:
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
}
func goBack(){
dismissViewControllerAnimated(true, completion: nil)
}
如果你想从firstView完成所有这些操作,那么这里是你的代码:
@IBAction func btnPressed(sender: AnyObject) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("OnePlayerCricketVC") as? OnePlayerCricketVC {
resultController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
let navController = UINavigationController(rootViewController: resultController) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)
}
}
func goBack(){
dismissViewControllerAnimated(true, completion: nil)
}