-1

I am trying to achieve the following. I have a navigation right bar item created programatically. Now i have a new view controller B. On click of right bar menu item i need to go to view controller B which is not connected through a segue. I want B to be having the navigation bar with back button.

Can anyone please guide how to do the same and oblige

rafavinu
  • 305
  • 4
  • 20

1 Answers1

1

Create your navigation bar item in ViewControllerA like this:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .Plain, target: self, action: "nextViewController")
}

Create a function in ViewControllerA to handle the touch event and navigate to ViewControllerB:

func nextViewController() {
    println("go next")

    //TODO: get an instance of your ViewControllerB here...

    //navigate to it
    self.navigationController!.pushViewController(viewControllerB, animated: true)   
}

How you get an instance of your ViewControllerB depends on how you defined it. If it's in your Main.storyboard, you can do this:

let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewControllerB") as? ViewControllerB

This requires you to set the Storyboard ID for your ViewControllerB to "ViewControllerB". Do this by selecting it in your storyboard and going to Identity inspector.

If your ViewControllerB is defined in a nib with the same name, do this:

let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)

If your ViewControllerB is defined entirely in code, this should work to get an instance of it:

let viewControllerB = ViewControllerB()
Mike Taverne
  • 9,156
  • 2
  • 42
  • 58