Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Push a New View Without Storyboards Swift

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)
}
like image 646
DMop Avatar asked Dec 31 '15 00:12

DMop


1 Answers

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

like image 195
Julian E. Avatar answered Nov 09 '22 19:11

Julian E.