Update 9/22/15: This tutorial was updated to iOS 9, Xcode 7, and Swift 2 by Caroline Begbie. Original post by Tutorial Team member Matthijs Hollemans.
If you want to learn about storyboards, you’ve come to the right place!
In the first part of this storyboards tutorial series, you covered the basics of using Interface Builder to create and connect various view controllers, along with how to make custom table view cells directly from the storyboard editor.
In this second and final part of this storyboards tutorial series, we’ll cover segues, static table view cells, the Add Player scene, and a game picker scene!
We’ll start where we left off last tutorial, so open your project from last time, or download the example code from the previous tutorial first.
OK, let’s dive into some of the other cool features in storyboards!
Introducing Segues
It’s time to add more view controllers to the storyboard. You’re going to create a scene that allows users to add new players to the app.
Open up Main.storyboard and drag a Bar Button Item into the right slot of the navigation bar on the Players scene with your table view. In the Attributes inspector change Identifier to Add to make it a standard + button.
When the user taps this button, you want the app to pop up a new modal scene for entering details of a new player.
Drag a new Table View Controller into the canvas to the right of the Players scene. Remember that you can double-click the canvas to zoom out so you have more room to work. With the new Table View Controller selected, choose Editor\Embed in\Navigation Controller.
Here’s the trick: Select the + button that you just added on the Players scene and ctrl-drag to the new Navigation Controller. Release the mouse button and a small popup menu shows up. Select present modally from the popup menu:
Reminder: You can’t add to or modify the contents of a storyboard when zoomed out. If you’re having issues creating the segue, try double clicking to zoom back in!
This places a new arrow between the Players scene and the Navigation Controller:
This type of connection is known as a segue (pronounce: seg-way) and represents a transition from one scene to another. The storyboard connections you’ve seen so far were relationships and they described one view controller containing another. A segue, on the other hand, changes what is on the scene. Segues are triggered by taps on buttons, table view cells, gestures, and so on.
The cool thing about using segues is that you don’t have to write any code to present the new scene, nor do you have to hook up your buttons to IBAction methods. What you just did, dragging from the Bar Button Item to the next scene, is enough to create the transition. (Note: If your control already had an IBAction connection, then the segue overrides that.)
Run the app and press the + button. A new table view will slide up the scene.
This is a so-called “modal” segue. The new scene completely obscures the previous one. The user cannot interact with the underlying scene until they close the modal scene first. Later on you’ll also see “show” segues that push new scene on the navigation stack of a Navigation Controller.
The new scene isn’t very useful yet – you can’t even close it to go back to the main scene. That’s because segues only go one way – so while it can go from the Players scene to this new one, it can’t go back.
Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next. There are three main steps:
- Create an object for the user to select, usually a button.
- Create an unwind method in the controller that you want to return to.
- Hook up the method and the object in the storyboard.
First, open Main.storyboard and select the new Table View Controller scene. Change the title of the scene to Add Player (by double-clicking in the navigation bar). Then add two Bar Button Items, one to each side of the navigation bar. In the Attributes inspector, set the System Item property of the button to the left to Cancel, and the one on the right to Done.
Next, add a new file to the project using the Cocoa Touch Class template – name it PlayerDetailsViewController and make it a subclass of UITableViewController
. To hook this new class up to the storyboard, switch back to Main.storyboard and select the Add Player scene. In the Identity inspector set its Class to PlayerDetailsViewController. I always forget this very important step, so to make sure you don’t; I’ll keep pointing it out.
Now you can finally create the unwind segue. In PlayersViewController.swift (not the detail controller), add the unwind methods at the end of the class:
@IBAction func cancelToPlayersViewController(segue:UIStoryboardSegue) { } @IBAction func savePlayerDetail(segue:UIStoryboardSegue) { } |
cancelToPlayersViewController(_:)
is simply a marker for the unwind segue. Later you’ll add code to savePlayerDetail(_:)
to allow it to live up to it’s name!
Lastly, switch back to Main.storyboard and hook up the Cancel and Done buttons to their respective action methods. Ctrl-drag from the bar button to the exit object above the view controller and then pick the correct action name from the popup menu:
Note the name that you gave the cancel method. When you create an unwind segue, the list will show all unwind methods (i.e. ones with the signature @IBAction func methodname(segue:UIStoryboardSegue)
) in the entire app, so ensure that you create a name that you recognize.
Run the app, press the + button, and test the Cancel and Done buttons. A lot of functionality for very little code!
Static Cells
When you’re finished with this section, the Add Player scene will look like this:
That’s a grouped table view, but you don’t have to create a data source for this table. You can design it directly in the storyboard — no need to write cellForRowAtIndexPath(_:)
for this one! The feature that makes this possible is called static cells.
Select the table view in the Add Player scene and in the Attributes inspector change Content to Static Cells. Change Style from Plain to Grouped and give the table view 2 sections.
Note: When you change the value of the Sections attribute, the editor will clone the existing section. (You can also select a specific section in the Document Outline on the left and duplicate it.)
The finished scene will have only one row in each section, so select two cells in each of the sections and delete them using the Document Outline.
Select the top-most Table View Section (from the Document Outline). In its Attributes inspector, give the Header field the value Player Name.
Drag a new Text Field into the cell for this section. Stretch out its width and remove its border so you can’t see where the text field begins or ends. Set the Font to System 17.0 and uncheck Adjust to Fit.
You’re going to make an outlet for this text field on the PlayerDetailsViewController
using the Assistant Editor feature of Xcode. While still in the storyboard, open the Assistant Editor with the button from the toolbar (the one at the top right with two intertwining rings). It should automatically open on PlayerDetailsViewController.swift (if it doesn’t, use the jumpbar in the right hand split window to select that .swift file).
Select the new text field and ctrl-drag to the top of the .swift file, just below the class definition. When the popup appears, name the new outlet nameTextField, and click Connect. After you click Connect, Xcode will add the property to the PlayersDetailViewController class and connect to it in the storyboard:
Creating outlets for views on your table cells is exactly the kind of thing I said you shouldn’t try with prototype cells, but for static cells it is OK. There will be only one instance of each static cell and so it’s perfectly acceptable to connect their subviews to outlets on the view controller.
Set the Style of the static cell in the second section to Right Detail. This gives you a standard cell style to work with. Change the label on the left to read Game by double clicking it and give the cell a Disclosure Indicator accessory.
Just as you did for the Name text field, make an outlet for the label on the right (the one that says “Detail”) and name it detailLabel
. The labels on this cell are just regular UILabel
objects. You might need to click a few times on the text “Detail” to select the label (and not the whole cell) before ctrl-clicking and dragging to PlayerDetailsViewController.swift
. Once done, it will look something like this:
The final design of the Add Player scene looks like this:
Note: The scenes you have designed so far in this storyboard all have the width and height of the 4.7-inch screen of the iPhone 6, which is 667 points tall. Obviously, your app should work properly with all the different screen sizes, and you can preview all these sizes within your Storyboard.
Open the Assistant Editor from the toolbar, and use the jump bar to select Preview. At the bottom left of the assistant editor, click the + symbol to add new screen sizes to preview. To remove a screen size, select it and hit the Delete key.
For the Ratings app, you don’t have to do anything fancy. It only uses table view controllers and they automatically resize to fit the screen space. When you do need to support different layouts for different sized devices, you will use Auto Layout and Size Classes.
Build and run now, and you’ll notice that the Add Player scene is still blank!
When you use static cells, your table view controller doesn’t need a data source. Because you used an Xcode template to create the PlayerDetailsViewController
class, it still has some placeholder code for the data source and that will prevent the static cells from working properly – that’s why your static content wasn’t visible here. Time to fix it!
Open PlayerDetailsViewController.swift and delete everything from the following line down (except for the class closing bracket):
// MARK: - Table view data source |
Run the app and check out the new scene with the static cells. All without writing a line of code – in fact, you threw away a bunch of code!
One more thing about static cells: they only work in UITableViewController
. Even though Interface Builder will let you add them to a Table View object inside a regular UIViewController
, this won’t work during runtime. The reason for this is that UITableViewController
provides some extra magic to take care of the data source for the static cells. Xcode even prevents you from compiling such a project with the error message: “Illegal Configuration: Static table views are only valid when embedded in UITableViewController instances”. Prototype cells, on the other hand, work just fine in a table view that you place inside a regular view controller.
Note: If you’re building a scene that has a lot of static cells — more than can fit in the visible frame — then you can scroll through them in Interface Builder with the scroll gesture on the mouse or trackpad (2 finger swipe). This might not be immediately obvious, but it does work.
You can’t always avoid writing code altogether though, even for a table view of static cells. When you dragged the text field into the first cell, you probably noticed it didn’t fit completely. There is a small margin of space around the text field. The user can’t see where the text field begins or ends, so if they tap in the margin and the keyboard doesn’t appear, they’ll be confused.
To avoid that, you should let a tap anywhere in that row bring up the keyboard. That’s pretty easy to do – just open PlayerDetailsViewController.swift and add a tableView(_:didSelectRowAtIndexPath:)
method as follows:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 0 { nameTextField.becomeFirstResponder() } } |
This just says that if the user tapped the first cell, the app should activate the text field. There is only one cell in the section so you only need to test for the section index. Making the text field the first responder will automatically bring up the keyboard. It’s just a little tweak, but one that can save users a bit of frustration.
Tip: when adding a delegate method, or overriding a view controller method, just start typing the method name (without preceding it with “func”), and then you will be able to select the correct method from the list available.
You should also set the Selection Style for that cell to None (instead of Default) in the storyboard Attributes inspector, otherwise the row appears highlighted if the user taps in the margin around the text field.
All right, that’s the design of the Add Player scene. Now let’s actually make it work.
The Add Player Scene at Work
For now you will ignore the Game row and just let users enter the name of the player.
When the user presses the Cancel button the scene should close and whatever data they entered will be lost. That part already works with the unwind segue.
When the user presses Done, however, you should create a new Player object and fill in its properties and update the list of players.
prepareForSegue(_:sender:)
is invoked whenever a segue is about to take place. You’ll override this method to store the data entered into a new Player object before dismissing the view.
Note: You never call prepareForSegue(_:sender:)
yourself. It’s a message from UIKit to let you know that a segue has just been triggered.
Inside PlayerDetailsViewController.swift, first add a property at the top of the class to hold details of the player that you are adding:
var player:Player? |
Next, add the following method to PlayerDetailsViewController.swift:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "SavePlayerDetail" { player = Player(name: nameTextField.text!, game: "Chess", rating: 1) } } |
prepareForSegue(_:sender:)
creates a new Player
instance with default values for game and rating. It does this only for a segue that has the identifier of SavePlayerDetail
.
In Main.storyboard, find the Add Player scene in the Document Outline and select the unwind segue tied to the savePlayerDetail
Action. Change Identifier to SavePlayerDetail:
Hop over to PlayersViewController and change the unwind segue method savePlayerDetail(segue:)
to look like this:
@IBAction func savePlayerDetail(segue:UIStoryboardSegue) { if let playerDetailsViewController = segue.sourceViewController as? PlayerDetailsViewController { //add the new player to the players array if let player = playerDetailsViewController.player { players.append(player) //update the tableView let indexPath = NSIndexPath(forRow: players.count-1, inSection: 0) tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } } } |
This obtains a reference to the PlayerDetailsViewController
via the segue reference passed to the method. It uses that to add the new Player
object to the array of players used in the datasource. Then it tells the table view that a new row was added (at the bottom), because the table view and its data source must always be in sync.
You could have just done tableView.reloadData()
but it looks nicer to insert the new row with an animation. UITableViewRowAnimation.Automatic
automatically picks the proper animation, depending on where you insert the new row. Very handy.
Try it out, you should now be able to add new players to the list!
Performance
Now that you have several view controllers in the storyboard, you might be wondering about performance. Loading a whole storyboard at once isn’t a big deal. The Storyboard doesn’t instantiate all the view controllers right away – only the initial view controller is immediately loaded. Because your initial view controller is a Tab Bar Controller, the two view controllers that it contains are also loaded (the Players scene from the first tab and the scene from the second tab).
The other view controllers are not instantiated until you segue to them. When you close these view controllers they are immediately deallocated, so only the actively used view controllers are in memory.
Let’s see that in practice. Add an initializer and deinitializer to PlayerDetailsViewController:
required init?(coder aDecoder: NSCoder) { print("init PlayerDetailsViewController") super.init(coder: aDecoder) } deinit { print("deinit PlayerDetailsViewController") } |
You’re overriding init?(coder:)
and deinit
, and making them log a message to the Xcode Debug pane. Now run the app again and open the Add Player scene. You should see that this view controller did not get allocated until that point.
When you close the Add Player scene, either by pressing Cancel or Done, you should see the print()
log statement from deinit. If you open the scene again, you should also see the message from init?(coder:)
again. This should reassure you that view controllers are loaded on-demand only.
The Game Picker Scene
Tapping the Game row in the Add Player scene should open a new scene that lets the user pick a game from a list. That means you’ll be adding yet another table view controller, although this time you’re going to push it on the navigation stack rather than show it modally.
Drag a new Table View Controller into Main.storyboard. Select the Game table view cell in the Add Player scene (be sure to select the entire cell, not one of the labels) and ctrl-drag to the new Table View Controller to create a segue between them. Make this a Show segue (under Selection Segue in the popup, not Accessory Action).
Select this new segue and give it the identifier PickGame in the Attributes Inspector.
Select the new Table View Controller in the Document Outline and in the Attributes Inspector, name this scene Choose Game.
Set the Style of the prototype cell to Basic, and give it the reuse identifier GameCell. That’s all you need to do for the design of this scene:
Add a new Swift file to the project, using the Cocoa Touch Class template and name it GamePickerViewController, subclass of UITableViewController.
Go back to your new Choose Game Scene in Main.storyboard and in the Identity Inspector, set its Custom Class to GamePickerViewController
.
Now let’s give this new scene some data to display. In GamePickerViewController.swift, add a games
string array property to the top populated with some hard coded values:
var games:[String] = [ "Angry Birds", "Chess", "Russian Roulette", "Spin the Bottle", "Texas Hold'em Poker", "Tic-Tac-Toe"] |
Now replace the data source methods from the template with:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return games.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("GameCell", forIndexPath: indexPath) cell.textLabel?.text = games[indexPath.row] return cell } |
Standard stuff here – you’re just setting up the data source to use the games
array and placing the string values in the cell’s textLabel.
That should do it as far as the data source is concerned. Run the app and tap the Game row. The new Choose Game scene will slide into view. Tapping the rows won’t do anything yet, but because this scene is presented on the navigation stack, you can always press the back button to return to the Add Player scene.
This is pretty cool, huh? You didn’t have to write any code to invoke this new scene. You just ctrl-dragged from the static table view cell to the new scene and that was it. The only code you wrote was to populate the contents of the tableView, which is typically something more dynamic rather than a hardcoded list.
Of course, this new scene isn’t very useful if it doesn’t send any data back, so you’ll have to add a new unwind segue for that.
At the top of the GamePickerViewController class, add properties to hold the name and the index of the currently selected game:
var selectedGame:String? { didSet { if let game = selectedGame { selectedGameIndex = games.indexOf(game)! } } } var selectedGameIndex:Int? |
Whenever selectedGame
is updated, didSet
will locate the game string in games
and automatically update selectedGameIndex
with the correct index into the table.
Next, change tableView(_:cellForRowAtIndexPath:)
to:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("GameCell", forIndexPath: indexPath) cell.textLabel?.text = games[indexPath.row] if indexPath.row == selectedGameIndex { cell.accessoryType = .Checkmark } else { cell.accessoryType = .None } return cell } |
This sets a checkmark on the cell that contains the name of the currently selected game. Small gestures such as these will be appreciated by the users of the app.
Now add the delegate method tableview(_:didSelectRowAtIndexPath:)
:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) //Other row is selected - need to deselect it if let index = selectedGameIndex { let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) cell?.accessoryType = .None } selectedGame = games[indexPath.row] //update the checkmark for the current row let cell = tableView.cellForRowAtIndexPath(indexPath) cell?.accessoryType = .Checkmark } |
This method is called by the Table View delegate whenever the user taps a row.
First this method deselects the row after it was tapped. That makes it fade from the gray highlight color back to the regular white. Then it removes the checkmark from the cell that was previously selected, and puts it on the row that was just tapped.
Run the app now to test that this works. Tap the name of a game and its row will get a checkmark. Tap the name of another game and the checkmark moves along with it.
The scene ought to close as soon as you tap a row but that doesn’t happen yet because you haven’t actually hooked up an unwind segue. Sounds like a great next step!
In PlayerDetailsViewController.swift, at the top of the class, add a property to hold the selected game so that you can store it in the Player object later. Give it a default of “Chess” so you always have a game selected for new players.
var game:String = "Chess" { didSet { detailLabel.text? = game } } |
didSet
will display the name of the game in the static table cell whenever the name changes.
Still in PlayerDetailsViewController.swift, add the unwind segue method:
@IBAction func unwindWithSelectedGame(segue:UIStoryboardSegue) { if let gamePickerViewController = segue.sourceViewController as? GamePickerViewController, selectedGame = gamePickerViewController.selectedGame { game = selectedGame } } |
This code will get executed once the user selects a game from the Choose Game Scene. This method updates both the label on screen and the game property based on the game selected. The unwind segue also pops GamePickerViewController off the navigation controller’s stack.
In Main.storyboard, ctrl-drag from the tableview cell to the Exit as you did before, and choose unwindWithSelectedGame: from the popup list:
In the Attributes Inspector give the new unwind segue the Identifier SaveSelectedGame.
Run the app to check it out so far. Create a new player, select the player’s game row and choose a game.
The game is not updated on the Add Player scene!
Unfortunately, the unwind segue method is performed before tableView(_:didSelectRowAtIndexPath:)
, so that the selectedGameIndex
is not updated in time. Fortunately, you can override prepareForSegue(_:sender:)
and complete that operation before the unwind happens.
In GamePickerViewController, override prepareForSegue(_:sender:)
:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "SaveSelectedGame" { if let cell = sender as? UITableViewCell { let indexPath = tableView.indexPathForCell(cell) if let index = indexPath?.row { selectedGame = games[index] } } } } |
The sender parameter of prepareForSegue(_:sender:)
is the object that initiated the segue, which in this case was the game cell that was selected. So you can use that cell’s indexPath to locate the selected game in games
then set selectedGame
so that it is available in the unwind segue.
Now when you run the app and select the game, it will update the player’s game details!
Next, you need to change PlayerDetailsViewController’s prepareForSegue(_:sender:)
to return the selected game, rather than the hardcoded “Chess”. This way, when you complete adding a new player, their actual game will be displayed on the Players scene.
In PlayerDetailsViewController.swift, change prepareForSegue(_:sender:)
to this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "SavePlayerDetail" { player = Player(name: nameTextField.text, game:game, rating: 1) } } |
When you complete the Add Player scene and press done, the list of players will now update with the correct game.
One more thing – when you choose a game, return to the Add Player scene, then try to choose a game again, the game you chose before should have a checkmark by it. The solution is to pass the selected game stored in PlayerDetailsViewController over to the GamePickerViewController when you segue.
Still in PlayerDetailsViewController.swift, add this to the end of prepareForSegue(_:sender:)
:
if segue.identifier == "PickGame" { if let gamePickerViewController = segue.destinationViewController as? GamePickerViewController { gamePickerViewController.selectedGame = game } } |
Note that you now have two if
statements checking segue.identifier
. SavePlayerDetail is the unwind segue going back to the Players list, and PickGame is the show segue going forwards to the Game Picker scene. The code you added will set the selectedGame
on the GamePickerViewController just before that view is loaded. Setting selectedGame
will automatically update selectedGameIndex
which is the index that the table view cell uses to set a checkmark.
Awesome. You now have a functioning Choose Game scene!
Where To Go From Here?
Here is the final Ratings iOS 9 example project with all of the code from the above tutorial.
Congratulations, you now know the basics of using the Storyboard Editor, and can create apps with multiple view controllers transitioning between each other with segues! Editing multiple view controllers and their connections to each other in one place makes it easy to visualize your app as a whole.
You’ve also seen how easy it is to customize table views and table view cells. Static cells make it easy to set up an interface without implementing all the data source methods.
If you want to learn more about storyboards, check out our book the iOS Apprentice or our iOS 101 video tutorial series.
If you have any questions or comments on this tutorial or on storyboards in general, please join the forum discussion below!
The post Storyboards Tutorial in iOS 9: Part 2 appeared first on Ray Wenderlich.