iOS Storyboards: Segues and More

In this tutorial, you’ll learn how to connect view controllers to a storyboard, programmatically trigger segues, control visual components and respond to the user interactions. By Ehab Amer.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

First Responders

A nice improvement is to have the text field for the player’s name automatically selected and just ready to type, instead of tapping the field each time you want to add a player.

In PlayerDetailsViewController.swift, add this line in viewDidLoad()

nameTextField.becomeFirstResponder()

This will give the text field focus just as if you tapped on it yourself.

Build and run and start adding a player. The screen will start with the keyboard open.

Note: Press Command-K to toggle the software keyboard on & off in the simulator.

Active Textfield

Performance With Storyboards

Storyboards don’t have any impact on your app’s performance. Although you designed all the view controllers in a single file, it doesn’t mean that they will all load in memory together. Only the view controller you’re showing will be loaded, and it will be released when you dismiss it.

In PlayerDetailsViewController.swift, add the following after the properties:

required init?(coder aDecoder: NSCoder) {
  print("init PlayerDetailsViewController")
  super.init(coder: aDecoder)
}

deinit {
  print("deinit PlayerDetailsViewController")
}

This is to track when a PlayerDetailsViewController instance initializes and deinitializes by printing some text in the log.

Build and run again. Open and close the add player scene. You’ll see the first log message when you open the screen and the second when you close it.

Storyboard Overview

Storyboard References

In bigger projects, the storyboard file can get crowded with many scenes. When working in a team, you’ll quickly learn that storyboard files can generate conflicts when more than one person is working on them.

But fear not, there is a remedy for that: Storyboard references. These allow you to break a storyboard into multiple files without losing any of the features illustrated so far.

As an example, you’re going to separate the flow of listing and adding the player.

Open Main.storyboard and zoom out so you can see all the scenes. Then drag a selection rectangle to select all the player-related scenes. Make sure you don’t select any additional scenes.

Scene Selection

Then, from the Xcode’s menu bar, choose Editor ▸ Refactor to Storyboard. Name the new file Player.storyboard. Xcode will create a new .storyboard file and will put the scenes you selected into it. In Main.storyboard, you’ll find a new item created.

Storyboard Reference

This item is referring to another set of scenes in another storyboard file. You can also insert a Storyboard Reference from the Object library if you prefer to link scenes yourself. If you build and run, you’ll that your app functions exactly as it did before.

Where to Go From Here?

In this tutorial, you only scratched the surface of what table views can do. To learn more about them you can watch the Beginning Table Views course.
The use of animations can also add a lot of value to your applications with a small amount of effort. It’s worth checking out the Beginning iOS Animations course. Animations work well with storyboards :]
Both courses are part of the iOS & Swift Beginner Path if you would like to learn more topics.

If you have any questions or comments, please don’t hesitate to join the forum discussion below.