Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set navigation bar?

Tags:

ios

swift

iphone

I am programmatically presenting a view controller, but when a view controller appears, it is missing a navigation bar. Is there any way to programmatically set a navigation bar on this new view controller when it is called?

Here is my code so far:

//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 :)

}
like image 570
Josh O'Connor Avatar asked May 11 '15 04:05

Josh O'Connor


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


1 Answers

Here is your code for initiate navigation controller from 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)
    }

EDIT:

If you wan to add back button into that navigation then use this code into OnePlayerCricket.swift class:

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)
}

If you want to do all of this from firstView then here is your code:

@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)
}
like image 116
Dharmesh Kheni Avatar answered Oct 18 '22 13:10

Dharmesh Kheni