6

First of all, I don't want to use storyboards at all. I am able to "present" the targeted view controller, but I can't get it to show with the standard iOS back button at the top. My understanding is I can get this to work by pushing the controller instead of presenting it. I don't get any errors, but nothing happens.

On a button click this code is run. The commented code is what successfully presented the ForgotPasswordPage :

// Changes to Forgot Password Page

func forgotPasswordSwitch(sender: UIButton!) { 
   //let ForgotPassword:ForgotPasswordPage = ForgotPasswordPage()
   //self.presentViewController(ForgotPassword, animated: true, completion: nil)
    let ForgotPassword = ForgotPasswordPage()
    self.navigationController?.pushViewController(ForgotPassword, animated: true)
}
DMop
  • 463
  • 8
  • 23
  • You are right - new need to use a navigation controller to be able to push a view controller. Since you are not using a storyboard you will need to create the UINavigationController instance yourself and use it to present your initial view controller – Paulw11 Dec 31 '15 at 00:08
  • @Paulw11 could you please elaborate? I declared both the page I am moving from and moving to as UINavigationControllers. What else do I need to do? – DMop Dec 31 '15 at 00:19
  • You don't declare your view controllers to be UINavigationController subclasses. A UINavigationController is a container view controller. You create one, assign your first view controller as its content and then present the UINavigationController. The class reference explains how it works https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/ – Paulw11 Dec 31 '15 at 00:21

2 Answers2

3

You have to manually create a UINavigationcontrollerto get the back bar. To do this you can use the code from this answer, which achieves this by using this code:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var mainView = ViewController() //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()

Here just add all the ViewControllers you want to be under the Navigation controller into the array and then push between them.

Hope that helps, Julian

Community
  • 1
  • 1
Julian E.
  • 4,687
  • 6
  • 32
  • 49
  • Thank you so much for your answer! I have a login page and I'm aiming to allow the user to click the "forgot password" or "account register" button and then have the option to click back at the top and go to the login page. Would I use this code to do that? This code seems to create a stack of all the view controllers in the array immediately. Then the code I have originally won't let me push between them when a button is clicked. – DMop Dec 31 '15 at 00:58
  • You could. It isn't the best approach but it would work. In this case I'd just suggest to programmatically create a navigation bar not controller and then simply dismiss the view controller when the back button was pressed. The back button in this case then should be a then a `BarButtonItem` that as action dismisses the view controller. However the code should work just fine. I hope that was clear. If you need any more elaboration feel free to ask :-) – Julian E. Dec 31 '15 at 01:03
  • Thanks! I'll give that a try! – DMop Dec 31 '15 at 01:22
0

I know that sometimes it's much better develop apps programmatically, but there are many time that Storyboard can save you a lot of time. You just simply use it for this situations...

1) In you Storyboard, localize the view controller you want to enable for pushing

2) Select it and find the "Editor" in your top bar

3) Select Editor->Embed In->Navigation Controller

And now your view controller it's ready for pushing

iGongora
  • 23
  • 4