Join Steven Lipton for an in-depth discussion in this video Create navigation controllers, part of iOS Development Tips Weekly.
- [Instructor] Not everyone loves a storyboard. With playgrounds, you can't use the storyboard. Often storyboards make for less portable code. Making user interfaces in code solves all these problems. If you do, you may need navigation controllers in code. Let me show you how to create navigation controllers both in playgrounds and in an Xcode project. In the downloads example file, I embedded a playground into an Xcode project so we can look at both projects and playgrounds. Open up the playground.
It's two view controllers, yellow and blue, connected through a BarButtonItem. In the action of the BarButtonItem, I'll make some code to navigate from the yellow controller to the blue controller. I'll first add an instance of the controller. Let bvc = BlueViewController and then I'll use the pushViewController method to show the BlueViewController. So for this, I'm going to put navigationController and that has the method pushViewController and the view controller that I'm going to push to is bvc and it asks for an animation which we're going to go yes, true.
Launching the view controller is that easy, but you need to set up the navigation controller too. There is only a few steps to make navigation controllers. The first step is to create the root view controller. This will be the controller where you start the app with. I'll go down to the bottom of the code, pass the classes, give myself some space, and I'll set the yellow view controller as my root view controller. Let rootViewController = and it'll be yellow.
Once I have the rootViewController, I can make a navigation controller with the root controller so let's do let navigationController = UINavigatonController and I'll select the one with the rootViewController and I just make that rootViewController. Now we'll just assign it as a live view. PlaygroundPage.current.liveView equals navigationController.
Go ahead and run this. I'll show the assistant editor and we see here's our yellow controller. I hit the blue button and we get blue. I hit the back button, I get yellow. You can do this to an Xcode project too, but it's slightly more work. Go to the Xcode project proper here and go to the YellowViewController. You'll see I set that up exactly the same way as I did before going to the blue controller. The blue controller has a little extra more.
I put another BarButtonItem in here to go to a red controller and then I have a red controller as well. I'll come back to red shortly, but first let's create the navigation controller. You make the navigation controller in the app delegate. So go to the app delegate and go to UIApplication didFinishLaunchingWithOptions and then make some space above the return true and we're going to make it pretty much the same way as we did in the playground by starting with let rootViewController and use the yellow controller here.
We'll use the same way we did it before. So you make your root controller. You'll make a navigationController object and that is a UINavigationController. And again, you'll assign it just the root controller and we use a rootViewController for this for our root controller just like we did in the playground.
Now you'll notice the top of the app delegate we've got this var window. Instead of the current playground pages live view, you'll assign the navigation controller to the window's root view controller property. So this time I'll put in window and then rootViewController equals navigationController. Now while this is the first window that should open, many developers get a little paranoid and make sure it is the top act of the window.
So we add one more command to this and that's window.makeKeyAndVisible which assures that's going to happen. If you run this, you get the yellow controller and I'll make this just a little smaller so we can see it all. Tap blue and you get blue. Tap red and you get red. You'll see a bar button on red called root. It does nothing right now, but I want to show you how to dismiss the controller without using the back button.
Go ahead and stop the app. Go to the RedViewController code. Navigation controllers are stacks so if you push a controller to present it, you pop it off the stack to remove it. In the root action add this, navigationController.pop. You see three choice in the auto complete. If you want to pop back to the last controller, you use popViewController. For controller further down the stack, you use popToViewController.
And if you want to go all the way back to the root, you do popToRootViewController which is what I'm going to use. So I go all the back to yellow and I'll set that to true for the animation. Go ahead and run this and hit blue, hit red, we're at the red controller, we hit root and we go back to yellow. With push, pop, and the navigation controller object, you can without a storyboard do everything you possibly want with navigation controllers without ever touching the storyboard.
With only the difference between the playground pages live view and the app delegate's window, it's very easy to port navigation controller code between playgrounds and Xcode projects.