Category Archives: Apps for iPhone

iPhone Apps (Swift) class from T1 2015-16

iOS Final GPA Calculator

Write an app that will calculate GPA for however many classes a user enters. I’m looking for students to show what they know–table views, arrays, auto layout… making good choices for a usable app. It is better to make something that works than something with lots of bells and whistles — he should make it work as well as he is able.
Users enter a grade, and it should figure out what the point value is (see below). They can either calculate the running GPA after each entry or with a separate button.
It would be good for the users to be able to clear the entries and start over.
Theo may look at his past apps–they should still be in his sent mail folder from sending them to me. If he has questions he is welcome to email me.
A+ or A: 4.0
A-: 3.7
B+: 3.3
B: 3.0
B-: 2.7
C+: 2.3
C: 2.0
C-: 1.7
D+: 1.3
D: 1.0
D-: 0.7
F: 0.0

Making a tableview cross off an entry on selection

Here’s the code I mentioned in class today that will make a table view cross off the row you tap on.  The purpose of posting this code is not for instruction or a requirement, but to help you explore something more with Tableviews.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let cell = tbl.cellForRowAtIndexPath(indexPath)! //gets the cell

        if cell.accessoryType == .Checkmark{  //checks to see if it’s already checked

            cell.accessoryType = .None  //turn off the checkmark

            let attribString = NSAttributedString(string: cell.textLabel!.text!,attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleNone.rawValue])  //create the ‘attributed text’ (that is, it has some styling attached to it) with the strikeout turned off

            cell.textLabel?.attributedText = attribString  //assign the new attributed text back to the textLabel

        }

        else  //it’s not already crossed off

        {

                cell.accessoryType = .Checkmark //add the checkmark

            let attribString = NSAttributedString(string: cell.textLabel!.text!,attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleThick.rawValue, NSStrikethroughColorAttributeName: UIColor.redColor()])  //create the attributed text with the strikout

            cell.textLabel?.attributedText = attribString  //assign the new attributed text to the label

        }

    }

Working with a tabbed App

in Xcode, we started today by creating a ‘tabbed application’ as opposed to a ‘Single View Application’.

The storyboard immediately looks different. It appears to have three screens, a tab view controller, a first view controller and a second view controller. In reality, the represents two screens on the phone – the tab view controller just manages the buttons at the bottom and handles the transitions between the two views.

The lines between the first and second controller represent ‘segues’ — that is, transitions from one view to the other. The straight arrow to the left of the tab view controller represents the starting point for this application. It should stay at the tab view controller.

A few notes about apps with multiple views. the ‘viewDidLoad’ method will only run the first time the view is loaded. If you need something to run every time the view shows up, use ‘viewDidAppear’ instead.

A variable declared in one viewcontorller.swift file will not be available in the other. There is an exception to this. If you declare a variable above the class declaration then it will be available in other .swift files in this app.

 

Managing the Keyboard

Today we worked on how to manage the keyboard.  There are three times when we want to dismiss the keyboard: when the user clicks our button, when the user clicks outside our textfield, and when the user clicks return on the keyboard.

  1. User clicks our button:
    1. Add the line txtFieldOutletName.resignFirstResponder() to your button action
  2. User clicks outside our text field
    1. You’ll need to add the ‘touchesBegan’ function to your code.  In a space where a func could go, start typing ‘touches’ and the XCode helper should give you a completion option.  Select it.
    2. Add the code to this function like this:  self.view.endEditing(true)
  3. User clicks return on the virtual keyboard
    1. ctrl click drag from text field to the yellow view controller icon at the top. Select ‘Delegate’
    2. Add “UITextFieldDelegate” to the class declaration
    3. Now you’ll be looking to add the function textFieldShouldReturn….  Start typing it into your code space and XCode should help you.
    4. Add the line txtFieldName.resignFirstResponder()
    5. Add the line return true

TableView Introduction

There are a lot of steps that go with making a table view work.  So we’re going to take them one by one.

 

  • Add a table view to your screen
  • Make the View Controller the ‘Data Source’ and the ‘Delegate’ for the tableview by ctrl-click-drag from the tableview to the yellow “view controller” icon at the top
  • Create the outlet for the table view in your code
  • Add the tableviewdelegate declaration to our class declaration
    • make your class declaration: 

      class ViewController: UIViewController, UITableViewDelegate {

Arrays and For loops

Important day in  iOS today.  We got Arrays and for loops.  Here is my playground code:

Here are the notes I displayed from my iPad:  Notes for arrays and for loops

//: Playground – noun: a place where people can play

import UIKit

//declaring an array

//start empty

var mail: [String] = [] // square brackets

mail.append(“Paycheck!”)

mail.append(“Speeding Ticket”)

mail.append(“Past Due Notice”)

print(mail.count)

print(mail.capacity)

print(mail[2])

//start with some literal values

var neighborsMail = [“Cell Bill”, “Letter from Mom”, “Card from Sweetheart”, “SW Airlines Ad”, “Credit Offer”]

print(neighborsMail[0])

mail.appendContentsOf(neighborsMail)

print(mail.count)

for (var n = 0; n < mail.count; n++){

    mail[n] = mail[n] + ” rcv’d”

    

    print(mail[n])

    

}

Auto Layout Demo

Auto Layout is hard to describe in words.  So I made this video that maybe will help out.  Remember, with each item, there are four things to think about:

  1. Width
  2. Height
  3. Vertical Position
  4. Horizontal Position

Also, you can use two horizontal position constraints (left and right) to make the width adjust with the width of the screen.