1

Normally, in storyboard I always do editor -> embedded navigation controller

is that the same with this code?

myNavigationController = UINavigationController(rootViewController: myController)

but the thing is how you attach the navigation controller just like in the storyboard?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

You should make the navigation controller the rootViewController of your main UIWindow.

If you create your interface entirely programmatically, it is common in you app delegate application:didFinishLaunchingWithOptions: to create both the main UIWindow and the main navigation controller, then assign the latter to the former's rootViewController property.

class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  var navigationController: UINavigationController?

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    self.navigationController = UINavigationController()

    var viewController: UIViewController = UIViewController()
    self.navigationController.pushViewController(viewController, animated: false)

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window.rootViewController = self.navigationController

    ...more initialisation...

    self.window.makeKeyAndVisible()

    return true
  }
}

EDIT:

Answering your question in the comment:

so is it the same when I create navigation controller from editor -> embedded in navigation controller? I mean like, if I do from editor-> embedded in navigation controller, will it produce the same result as I do programatically?

it depends on how you look at it. from a user viewpoint, I would say, yes. from a programmer's viewpoint, I would say not, because the storyboard will generate a bunch of other objects (related to the scenes, segues, etc.)

In other words, if you are only concerned with creating a navigation view controller and attaching to your window, the storyboard will do that just like I outlined it above (plus the bunch of things that I mentioned before and that do not change the end result of "attaching", just the way of doing it).

If you mean you want to recreate the very same settings as those created in the storyboard, I don't think you can create a new storyboard scene programmatically, because Apple does not make public all the relevant classes.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • so is it the same when I create navigation controller from editor -> embedded in navigation controller? I mean like, if I do from editor-> embedded in navigation controller, will it produce the same result as I do programatically? – user2557607 Jan 16 '15 at 00:18
  • Please, see my edit. Also, perhaps the end aim of your asking is not entirely clear. If my answer does not help, think of making it clearer *why* you are asking this: just curiosity? you have some concrete task in mind? – sergio Jan 16 '15 at 09:32