Make App Pie

Training for Developers and Artists

Swift Swift: UI View Controllers

Screenshot 2015-04-01 07.53.14

It was hard to tell if the cheers of joy or cries of fear were louder when Swift was announced, the next generation programming language to provide rapid development and more error free code than its predecessor Objective-C. There is nothing to fear. Grabbing the best from many different languages Swift makes for a completely new, faster and better programming experience which you will come to love.
Not as noticed in iOS8, but with as big an impact was size classes powered by auto layout. Size classes change the use of story boards, and present view controllers in new ways programmatically to make truly universal apps without extra code for each device.
In this book, you will learn to use auto layout, size classes and new Swift implementations for all the major types of view controllers including:

  • Modal views
  • Popovers
  • Alerts
  • Action Sheets
  • Navigation Controllers
  • Tab Bar Controllers
  • Table Views
  • Split Views
  • Page views
  • Collection Views

With plenty of color illustrations and code snippets, Swift Swift View Controllers will take you step by step through many easy demonstrations, teaching you the stuff you really need to know to implement any of these view controllers. The book will help you understand how to move data between all these view controllers with delegates, segues and more for an efficient and well-written app.
If you are an intermediate programmer getting into auto layout or Swift’s view controllers, or you just finished a beginning guide to Swift and want more, Swift Swift: UI View Controllers will be invaluable.
Download_on_iBooks_Badge_US-UK_110x40_090513

 

Downloading Source Code and Assets

NOTE: All the source code available below is for Swift 1.0.  It is obsolete. If you want to work through converting it to Swift 3.0 I’d suggest  watching the WWDC2016  presentation Swift API design guidelines.  I will be updating the code as I finish the Xcode 8 version of the book. That process will begin in November 2016.  

I’m paranoid about zip files from strangers and of sending too much information. It’s too easy to stick something not so good in something. I feel that keeping what you download from me as secure as possible is a good thing. I also know a lot of people who can’t figure out Github. So I get old-fashioned about this: I send only source code files when possible and keep my downloads small.
You will find downloads on the website for each chapter as a .zip file. There will be .swift files and one .storyboard file in each exercise. In a few cases there will be .png files which are graphics you need for the demo. For example, for section 2 of Chapter Two you will find the following files:
Main.storyboard
Pizza.swift
ViewController.swift

Open a new project as described in each of the sections. Once you have a blank project, send ViewController.swift and Main.storyboard to the garbage can. From the place you downloaded and unpacked the source code, drag the code into the new project’s navigator. You will get the following:

Screenshot 2015-04-08 08.13.02
Make sure Copy items if needed is checked and click Finish. Your source files are transferred. For graphics, follow the directions in the section for each graphic’s use.

Book Resources

Complete asset and source code in one zip file:

Chapter 1 :Introduction

In chapter 1 I introduce what we will cover in the rest of the book, and how to read and use the code found throughout the book.

Chapter 2: MVC and Connections

In Chapter 2 I explain the Model View Controller programming pattern, a pattern so fundamental Xcode forces you to use it. I explain what MVC is, why to use it, and then using navigation controllers demonstrate how to communicate between view controllers using MVC. I then explain the most misunderstood but necessary programming pattern of all for iOS programming: delegation.

Assets

Section 1 :  The Model View Controller Pattern

Section 2 : A Model View Controller Example

SwiftMVCPizzaDemo2_Sources

Section 3 : Using Segues

SwiftMVCPizzaDemo3_Sources

Section 4 : Protocols and Delegation

SwiftMVCPizzaDemo4_Sources

Section 5 : Property Observers

SwiftMVCPizzaDemo5_Sources

Chapter 3: Auto Layout and Size Classes

In Chapter 3 we’ll introduce size classes. As a prerequisite, we’ll introduce and use auto layout. We’ll work through a universal layout that displays differently on different devices on a single storyboard.

Section 1: Beginning Auto Layout

Section 2: Working with Auto Layout

MySizePizzaDemo_NoLayout_Sources

MySizePizzaDemo_2_Sources

Section 3: Starting with Size Classes

Section 4: Compact Size Classes

MySizePizzaDemo_4_Sources

Section 5: Regular Class Sizes

MySizePizzaDemo_5_Sources

Section 6: Fixing Conflicting Constraints

MySizePizzaDemo_6_Sources

Chapter 4: Modal View Controllers

In Chapter 4, we discuss modal view controllers. We show how to present them from the storyboard, and then a xib. We discuss how size classes relate to their presentation, as well as transition styles. There are three special controllers that are now modal controllers in iOS8: popovers, alerts and action sheets. We complete the chapter discussing each of these.

Section 1: Presenting Modal Controllers

SwiftPizzaModal_SourcesAssets

 CH4_Assets

Section 2: Xibs and Modal Delegates

SwiftXibModalDemo_Sources

Section 3: Presenting Modals Universally

SwiftRegularModalDemo_Sources

Section 4: Using Popovers

SwiftPizzaPopoverDemo_Sources

Section 5: Using Alerts and Action Sheets

SwiftAlertActionDemo_Sources

Assets

 CH4_Assets

pizza_sm@2x

pizza_sm
pizza_sm

Chapter 5: Embedded Controllers

In Chapter 5 we discuss two controllers that affect navigation: navigation controllers and tab bar controllers. As many topics about navigation controllers get covered in the fundamentals, we discuss some programmatic and customizing techniques for navigation controllers. The rest of the chapter covers setting up and sharing data in tab bar controllers.

Section 1: Navigation Controllers

SwiftProgNavControllerDemo_Sources

Section 2: Tab Bar Controllers

CH5_Assets

SwiftTabBarEmbed_Sources

Section 3: Coding Tab Bar Controllers

CH5_Assets

SwiftTabProgDemo_Sources

Section 4: Models and Tab Bar controllers

SwiftTabDataOrderDemo_Sources

Assets

CH5_Assets

pizza bar icon@2x pizza bar icon pie bar icon@2x pie bar icon gradient bar@2x gradient bar

 

Chapter 6: Delegate Controllers

Some controllers use delegates and data sources for much of their operation. In Chapter 6 we cover how to use these controllers: table view controllers, the split view controller, the page view controller and the collection view controller. We discuss how to implement them, configure their appearance and how to use data with them.

Section 1: Table view Controllers

 SwiftTableViewDemo_Sources

CH6_Assets

Section 2: Split View Controllers

SwiftTemplateSplitView_Sources

SwiftSplitColorDemo_Sources

SwiftSplitViewCrash_Sources

Section 3: Page View Controllers

 SwiftPagePizzaDemo_Sources

Section 4: Collection View Controllers

 SwiftPizzaCollectionView

23 responses to “Swift Swift: UI View Controllers”

  1. Erratum code (Chapter 2, Section 5: SwiftMVCPizzaDemo5_Sources): In the method priceDidFinish in the ViewController class, the pizza delegate needs to be set back to the ViewController instance: i.e. pizza.delegate = self

    If not, the property observers won’t update the current view when altering pizza properties after returning from the PriceVC

  2. Erratum code (Chapter 6.1 The Model Delegate)

    Missing a call to tableView.reloadData() in the didSelectMenuItem() delegate function in RootTableViewController.swift

    // MARK: – Delegates
    func didSelectMenuItem(controller: UITableViewController, order: OrderModel) {
    orderList.addList(order)
    tableView.reloadData()
    controller.navigationController?.popViewControllerAnimated(true)
    }

    Without the reloadData() call the Save button will pass the order back to the RootTableViewController just fine but won’t display.

    This line of code is missing in both the book content and the resource files and is particularly irksome as all of the subsequent custom UITableViewCell formatting is all in the Order List table display.

    Really enjoying the book so far but it needs to be updated for Swift 1.2 changes in order to get successful compiles/builds.

    There are also a number of typos that should be corrected. One example is in this same chapter 6 where PizzaMenuTableViewController.swift is created but then frequently referred to as PizzaMenuViewController, .e.g. ‘Table’ is missing.

    1. Thank you. I will try to get those corrected as soon as possible. I’m aware of the reloadData issue.

  3. Dr. Scott B Steinman Avatar
    Dr. Scott B Steinman

    This page has a heading stating that the code and assets can be downloaded in a single zip file, but there’s no link. Could you please provide the link? Thanks!

    1. Unfortunately there is no link because ther is no file. The copy in the website was wrong. Due to lots of changes in the Swift language, it is near impossible to keep an up,to date single zip file. the current chapter files are mostly correct, but the are a few I am updating this week to Swift 1.2

  4. On the page 115,
    Drag the picture pizza_em does not work. Tell me what is wrong.
    I drag the UIImage view from object library. But he won’t receive image.
    Xcode is 6.4

    1. You can also drag an UI image controller into the new view controller scene then change its image to pizza_sm.

  5. running Xcode7, for Chapter 2 section 2 getting the error:

    Thread 1:signal SIGABRT

    In the app delegate screen

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

    1. You most likely have an outlet or action connected improperly

  6. Hi Steven. I have read auto layout and it was amazing. Any chance you could update Swift View Controllers for Swift 2.0? Feels like there have been some massive changes from swift 1.2 to Swift 2.0 and its tough to pay $20 for a book slightly out of date.
    Thanks
    Edward

    1. I’m working on it. As soon as the Autolayout is out the door, I’m going to start working on it. Unlike auto layout, it’s a lot easier edit. Apple is different than Amazon in that I can update books instead of having to write brand new ones. I’m planning to have it done by the end of the year, while I’m writing the WatchOS2 book which also has to be done by the first of the year.

  7. I love iBooks, but I am hasitating to buy it. because, considering the TOC, it does not cover about nested view contollers that divide the screen or switching between them patially using conainder view contoller. I would be nicer if you cover more advanced topic. Updating it rather than new book would be greatful.
    and let me know if you also publish Cocoa Development for OS X books.

    1. Thanks for the input. There’s a lot of things missing that would be advanced topics. There’s a few things I’m still adding, such as custom table views. I’ll have to consider that for a future edition.

      I have no plans to work on an OSX book. I got enough on my plate with iOS and Apple’s annual version changes.

  8. Are you aware of the problem that the iPhone 6+ simulator ( and perhaps the iPhone 6+ ) does (do) not present a popover in landscape mode because the traits collection incorrectly returns a horizontal value of “compact”, and not “regular”.

    Check out “Radar: Incorrect Horizontal Size Class” (ref: 18893122)

    P.S. I’m enjoying the book but unfortunately it has many typos and misprints.

    1. I was, and I thought that was in there. Maybe I’m mistaken and I discuss this in Practical Auto Layout. I’ll do another round of typo checks when Xcode 7.3 posts and I update for Xcode 7.3. If you want to put together a list of waht you found I’d be grateful for the help.

    2. Actually now that I think about it I mentioned it but didn’t put a workaround in. Maybe that would be something I should do. Hmmmm. Thanks for the idea. It’s hard trying to write two more books, keep the two I have up to date and run a website with new weekly content in three hours a day and $1000/yr budget. I’ll try to do better. I look forward to the day I can afford an editor.

  9. […] this system and develop an app to demonstrate MVC from the Xcode 8/Swift 3.0 edition of my book Swift Swift View Controllers coming in October, though available now for Xcode 7/Swift […]

  10. Hi Steve,
    I recently bought your iBook ‘SWIFT SWIFT: UI View Controllers’ after finding your other book ‘Practical Auto Layout’ extremely informative and useful. I am running the latest XCODE 8, but even after loading your source code for Chapter 2, Section 2 A Model View Controller Example I get the following errors in ViewController.swift from the compiler. I tried making the compiler suggestions, but to no avail.
    1)
    pizza.pizzaType = sender.titleForSegmentAtIndex(index)!
    … ‘titleForSegmentAtIndex’ has been renamed to ‘titleForSegment(at:)’
    2)
    An error in the sizeButton Function:
    pizza.pizzaDiameter = pizza.diameterFromString(sender.titleLabel!.text!)//2
    … Missing argument a label ‘aString:’ in call
    3)
    An error in the displayPizza Function:
    resultsDisplayLabel.text = displayString
    ‘NSString’ is not implicitly convertible to ‘String’; did you mean to use ‘as’ explicitly convert?

    Can you please either update the source code so it works with XCODE 8 or provide the necessary corrections please?

    Cheers John

    1. My apologies for that. It’s in the works. I’m trying to get another book that is Affected by Xcode 8 out first and that should be next week. IAs soon as that is done, I’ll try to get updates Chapter by chapter out for View controllers. THe others book comprises chapter 3 of View Controllers, so you will be getting that in the updates too.

      Again my apologies

    2. In the meantime here’s how to fix those errors:

      1)
      pizza.pizzaType = sender.titleForSegmentAtIndex(index)!

      That should be

      pizza.pizzaType = sender.titleForSegment(at:index)!
      2)
      An error in the sizeButton Function:
      pizza.pizzaDiameter = pizza.diameterFromString(sender.titleLabel!.text!)//2
      … Missing argument a label ‘aString:’ in call

      You have two options: change the function to
      func diameterFromString(_aString:sender.titleLabel!.text!)

      The underscore will hide the parameter. Or you can change each call of the function to this
      pizza.diameterFromString(aString:sender.titleLabel!.text!)
      3)
      An error in the displayPizza Function:
      resultsDisplayLabel.text = displayString

      Remove all declarations for NSString. I can’t find where you are getting a NSString because in the Xcode 7 edition everything should be the Swift String Type.

  11. Many thanks Steve for the quick response, I will continue with the book which I am finding very interesting.

    Cheers John

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.