Let’s start as usual by making a new project, following Create a Place to Play with Tests but using the project name Navigation. And delete its initial test file NavigationTests.swift.
Open Main.storyboard. Since we’ll be experimenting with push navigation (as well as modal navigation), we need to place the view controller within a navigation controller. Select the view controller scene. Then in the Xcode menu, select Editor ▶ Embed In ▶ Navigation Controller.
We’re going to work with push navigation and modal presentation for both code-based and segue-based approaches. Let’s add four buttons to trigger each form of navigation. Edit ViewController.swift to add four button outlets, declaring them private(set) so that tests can reach them:
| @IBOutlet private(set) var codePushButton: UIButton! |
| @IBOutlet private(set) var codeModalButton: UIButton! |
| @IBOutlet private(set) var seguePushButton: UIButton! |
| @IBOutlet private(set) var segueModalButton: UIButton! |
Back in the storyboard, open the Object Library and add four buttons to the view controller scene:
Position them as you like. Connect each to its @IBAction in ViewController.