Intro to IOS - HOTH 7 Workshop README

Slides: https://tinyurl.com/hoth7iosworkshop

Image: https://imgur.com/a/u1fm6

Table of Contents

  1. Begin by creating a new project in Xcode.
  2. Select "Create a new Xcode project" near the bottom left of the window.

open img


  1. Ensure that the "iOS" tab at the top of the page is selected.
  2. Double-click on "Single View App".

img


  1. Name your project "BruinTap".
  2. Pick whatever Team, Organization Name and Organization Identifier that you want.
  3. Ensure that the Language field is set to "Swift".
  4. Ensure that the User Interface is set to "Storyboard".
  5. Ensure that "Use Core Data" is unchecked.
  6. Click "Next".
  7. On the following window, select where you'd like your project's folder to be created (default location is ~/Desktop) and click "Create".

img


  1. In the Project Navigator on the left, you should see all of the files associated with our project. The only group (folder) we're interested in for the purposes of this tutorial is the BruinTap group. The BruinTapTests, BruinTapUITests, and Products groups aren't important for now.
  2. Inside the BruinTap group you should see a file named Main.storyboard. Click it and you'll be taken to the Interface Builder.


  1. Locate the plus sign on the top right of the screen and click it.
  2. The Object Menu will appear and contain UI elements we can drag into our storyboard.


  1. Locate the "Label" and "Button" elements, which should be the first two entries in the menu.


  1. Drag two Labels and two Buttons into the View Controller, and arrange them as they are in the following photo (it doesn't have to be exact, just more or less):


  1. Select the Button we placed in the top left corner.
  2. On the right side of the screen, open the Attributes Inspector by clicking the tab to the left of the ruler icon:


  1. Under the "Title" field (third from the top), change the text from "Button" to "Start" and press Enter.


  1. With the Start button still selected, click the "Add New Constraints" button near the bottom right of the page.


  1. Set both the top and left margins to 30. The red lines should now be solid rather than dashed.
  2. Click "Add 2 Constraints".


  1. Now select the Button in the middle of the page.
  2. Click the "Add New Constaints" button near the bottom right of the page.
  3. Set the Width and Height to 150. The boxes next to Width and Height should now be checked.
  4. Click "Add 2 Constraints".


  1. With the Button in the middle of the View Controller still selected, click the "Align" button directly to the left of the "Add New Constraints" button near the bottom right of the page.
  2. Check both the "Horizontally in Container" and "Vertically in Container" boxes.
  3. Click "Add 2 Constraints".


  1. Select the Label above the Button in the middle of the page.
  2. In the Attributes Inspector on the right side of the screen (the tab to the left of the ruler icon), change the Text field to 0 (then press Enter) and change the Alignment to centered.


  1. With the top label still selected, click the "Add New Constaints" button near the bottom right of the page.
  2. Set the bottom margin to 75. The red line should now be solid rather than dashed.
  3. Click "Add 1 Constraint".


  1. With the top label still selected, click the "Align" button directly to the left of the "Add New Constraints" button near the bottom right of the page.
  2. Only check the "Horizontally in Container" box.
  3. Click "Add 1 Constraint".


  1. Now select the Label below the Button in the middle of the page.
  2. In the Attributes Inspector on the right side of the screen (the tab to the left of the ruler icon), change the Text field to 10.00 (then press Enter) and change the Alignment to centered.


  1. With the bottom label still selected, click the "Add New Constaints" button near the bottom right of the page.
  2. Set the top margin to 75. The red line should now be solid rather than dashed.
  3. Click "Add 1 Constraint".


  1. With the bottom label still selected, click the "Align" button directly to the left of the "Add New Constraints" button near the bottom right of the page.
  2. Only check the "Horizontally in Container" box.
  3. Click "Add 1 Constraint".


  1. All set, verify that your View Controller now looks like this:


  1. In the Project Navigator on the left, select the folder icon named Assets.xcassets.
  2. In the left pane (the one containing the "AppIcon" entry), right click and select "New Image Set".


  1. Single click our newly created Image and rename it to ButtonBG.


  1. Click here and download the image.
  2. From Finder/your Downloads, drag the downloaded image into the slot in ButtonBG labeled 1x.


  1. Return via the Project Navigator to Main.storyboard.
  2. Select the Button in the center of the View Controller and set its Title field to nothing/empty via the Attributes Inspector.


  1. Five fields down from the "Title", use the drop-down menu to change the "Background" to ButtonBG.


  1. Click on the stack of lines near the top right of the View Controller scene (highlighted in red in the first screenshot) and click on "Assistant" to open the Assistant Editor.
  2. If it is difficult to see our View Controller, close the left pane if necessary by clicking on the button (square with black rectangle on the left side) to the left of "View as iPhone..." in the bottom left of the window.


  1. While holding down control (not to be confused with cmd(⌘)), click on the "Start" button and drag to the space directly above the viewDidLoad() function.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. Set the "Name" field to startButton.
  4. Click "Connect".


  1. While holding down control, click on the button in the middle of the View Controller and drag to the space directly below the startButton line we just made.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. Set the "Name" field to tapButton.
  4. Click "Connect".
  5. While holding down control, click on the "0" label and drag to the space directly below the tapButton line we just made. (Note: You may have to zoom in via pinch-drag on your touchpad to be able to select the label itself).
  6. In the window that appears, ensure that the "Type" field says UILabel, else press "Cancel" and try again.
  7. Set the "Name" field to scoreLabel.
  8. Click "Connect".
  9. While holding down control, click on the "10.00" label and drag to the space directly below the scoreLabel line we just made. (Note: You may have to zoom in via pinch-drag on your touchpad to be able to select the label itself).
  10. In the window that appears, ensure that the "Type" field says UILabel, else press "Cancel" and try again.
  11. Set the "Name" field to timeLabel.
  12. Click "Connect".
  13. Add a new line under the "timeLabel" declaration we just made if necessary (for neatness).
  14. You should see the following:


  1. While holding down control, click on the "Start" button and drag to the space directly below the viewDidLoad() function.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. If the "Connection" field is set to "Outlet", use the drop-down menu to change it to Action.
  4. Set the "Name" field to tappedStartButton.
  5. Use the drop-down menu to change the "Type" field from Any to UIButton.
  6. Ensure that the "Event" field is set to Touch Up Inside and that the "Arguments" field is set to sender.
  7. Click "Connect".


  1. While holding down control, click on the Button in the middle of the View Controller and drag to the space below the function we just made, but still above the viewDidLoad() function.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. If the "Connection" field is set to "Outlet", use the drop-down menu to change it to Action.
  4. Set the "Name" field to tappedTapButton.
  5. Use the drop-down menu to change the "Type" field from Any to UIButton.
  6. Ensure that the "Event" field is set to Touch Up Inside and that the "Arguments" field is set to sender.
  7. Click "Connect".
  8. Add newlines as necessary for neatness.
  9. You should see the following:


Coding in Swift

  1. Close the Assistant Editor by clicking on the "X" on the top right of the pane with our code in it.


  1. In the Project Navigator on the left side of the screen, select the "ViewController.swift" file.


  1. Above all of the IBOutlet statements, but still within the (class ViewController block), add the following code:
var score = 0
    
var timer: Timer!
var time: Double = 10


  1. Inside the tappedTapButton() function, add the following code:

    score += 1
    scoreLabel.text = String(score)


  1. Use the "Play" button in the top left of Xcode to build and test our app out. Selecting a smaller phone like the iPhone 8 will make it easier to see everything.
  2. Try pressing the button. The score should go up each time we tap it!


  1. Inside ViewController.swift, declare a function exposed to Objective-C after tappedTapButton() named timerAction() and fill it with the following code:

    @objc func timerAction() {
            
            time -= 0.01
            timeLabel.text = String(format: "%.2f", time)
            
            if time <= 0 {
                
                timer.invalidate()
                timeLabel.text = "0.00"
                
                startButton.isEnabled = true
                
            }
            
    }
  2. Inside the tappedStartButton() function, add the following code:

    time = 10
    timeLabel.text = String(format: "%.2f", time)
            
    timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
            
    startButton.isEnabled = false


  1. Build and run our app again. Tap the "Start" button and you should see the timer counting down!
  1. Inside the viewDidLoad() function, add the following code:

    tapButton.isEnabled = false
  2. Change the timerAction() function to the following:

    @objc func timerAction() {
            
            time -= 0.01
            timerLabel.text = String(format: "%.2f", time)
            
            if time <= 0 {
                
                timer.invalidate()
                timerLabel.text = "0.00"
                
                startButton.isEnabled = true
                tapButton.isEnabled = false
                
            }
            
    }
  3. Change the tappedStartButton() function to the following:

    @IBAction func tappedStartButton(_ sender: UIButton) {
            
            score = 0
            time = 10
            
            scoreLabel.text = String(score)
            timeLabel.text = String(format: "%.2f", time)
            
            timer = Timer.scheduledTimer(timeInterval: 0.01, 
                                         target: self, 
                                         selector: #selector(timerAction), 
                                         userInfo: nil, 
                                         repeats: true)
            
            startButton.isEnabled = false
            tapButton.isEnabled = true
            
    }
  4. Build and run the app again.

Add Another View Controller

  1. In the Project Navigator on the left, select the Main.storyboard file.
  2. Locate the plus sign on the top right of the screen and click it to reveal the Object Menu.


  1. Locate the "View Controller" object and click and drag one to the right of the View Controller we've already made.

  2. Scroll and locate the "Label" and "Button" objects, then drag a label and a button into the new view controller we created and one button into the old view controller like so:


  1. Select the button we placed in the top right corner of the old view controller.
  2. Use the Attributes Inspector to change its text to "What is Love".
  3. With the button still selected, click the "Add New Constraints" button near the bottom right of the page.
  4. Set the top and right margins to 30. The red lines should now be solid rather than dashed.
  5. Click "Add 2 Constraints".


  1. Select the button we placed in the top left corner of the new view controller.
  2. Use the Attributes Inspector to change its text to "Close".
  3. With the button still selected, click the "Add New Constraints" button near the bottom right of the page.
  4. Set the top and left margins to 30. The red lines should now be solid rather than dashed.
  5. Click "Add 2 Constraints".


  1. Select the label we placed in the middle of the new view controller.
  2. Use the Attributes Inspector to change its text to "Vim > Emacs". This step is very important!
  3. With the button still selected, click the "Align" button near the bottom right of the page.
  4. Center the label horizontally and vertically
  5. Click "Add 2 Constraints".


  1. Select the "Spill the Tea" button we placed in the old view controller.
  2. While holding down control (not to be confused with cmd(⌘)), click on the "Spill the Tea" button and drag to the new view controller.
  3. In the window that appears, select "Present modally".


  1. In the Project Navigator on the left, select the ViewController.swift file.
  2. Copy the following code into the class (make sure you copy it outside of all of the other functions we've written).
@IBAction func unwind(segue: UIStoryboardSegue) { }

  1. Use the Project Navigator to go back to the Interface Builder by selecting the Main.storyboard file.
  2. While holding down control (not to be confused with cmd(⌘)), click on the "Close" button and drag to the "Exit door" icon on the top of the new view controller.


  1. Build your app and run. Congratulations, you've finished your first app!