1

In a Swift 3 project using Xcode 8.3 and iOS 10.3, I get a navigation controller to push. The app runs with a navigation controller until I try to use it to push. All works until the last line which cause an app crash, fatal error: unexpectedly found nil while unwrapping an Optional value. I don't want to use the storyboard, the point of this question is how to accomplish this task without the storyboard.

App Delegate code

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)

        window?.makeKeyAndVisible()

        window?.rootViewController = UINavigationController(rootViewController: MainViewController())

}

View Controller:

import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        let navController = navigationController

        let myProfileViewController = MyProfileViewController()

        navController!.pushViewController(myProfileViewController, animated: true)

    }
}        
markhorrocks
  • 1,199
  • 19
  • 82
  • 151

3 Answers3

2

Move

let navController = navigationController
let myProfileViewController = MyProfileViewController()

navController!.pushViewController(myProfileViewController, animated: true)

from viewDidLoad to viewDidAppear.

In viewDidLoad there is no parent navigation controller set up and you should not start animations from there.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

You can add navigation controller in storyboard, in Storyboard click on MainViewController & from editor menu add navigation controller & push via navigation controller as below code.

  let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyProfileViewController") as! MyProfileViewController
    self.navigationController?.pushViewController(vc, animated: true)

Make sure storyboard identifier is correct. & remove navigation controller code form AppDelegate class.

Jack
  • 13,571
  • 6
  • 76
  • 98
  • I don't want to use the storyboard. I do know how to do this in the storyboard, I want to know how to do this programmatically but thank you for your response. – markhorrocks Apr 14 '17 at 14:42
0

first give name to your navigation controller like this enter image description here

then in your code initialise it like this let nav = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigation") as! UINavigationController); nav.pushFrontViewController(MainViewController(), animated: true); window?.rootViewController = nav

adam
  • 193
  • 1
  • 9
  • My nav controller was created programmatically, there is nothing in the storyboard. – markhorrocks Apr 14 '17 at 15:30
  • That's all of it above. It should be sufficient to reproduce the error. – markhorrocks Apr 14 '17 at 19:07
  • well then then the problem you are facing is that you have declared your MainViewController but UINavigationController is not been declared or initialised, i am sorry but my expertise is limited – adam Apr 14 '17 at 19:13
  • Then how do I do that? Please write up your reply as an answer. – markhorrocks Apr 14 '17 at 19:15
  • there is one solution http://stackoverflow.com/questions/28793331/creating-a-navigationcontroller-programatically-swift see if it helps – adam Apr 14 '17 at 19:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141739/discussion-between-adam-and-markhorrocks). – adam Apr 14 '17 at 19:23