Coder Social home page Coder Social logo

git-swift-todo-tutorial's Introduction

git-swift-todo-tutorial

Foreword

  1. We are obligated by an NDA not to post any screenshots. Sorry! We'll add them when the NDA lifts.
  2. Much of xCode 6 is in flux - we are developing for xCode 6 Beta 3. We will update as we go and update that message.
  3. This tutorial is based on apple's xcode 5 todo tutorial, which is an excellent place to look for auxilliary context (and screenshots!)
  4. Check out the Todo folder for the completed code

Installing This App

  1. cd /your/preferred/xcode/location
  2. git clone https://github.com/jadekler/git-swift-todo-tutorial.git
  3. Open xcode
  4. File > Open > /your/preferred/xcode/location/git-swift-todo-tutorial/Todo

Feedback

Please, provide feedback on anything you feel is lacking or could be better worded! Pull requests, github issues, or direct emails to [email protected] are all welcome!

Getting Start: The Basics

Create your project
  1. Open xCode
  2. Under iOS > Application, select 'Single View Application' and click Next
  3. Type 'Todo' as the Product Name, Swift as the Language, iPhone as the Devices, 'self' for Organization Identifier, and make sure Core Data is unchecked
  4. Save into a folder of your choosing
  5. Choose 'iPhone 4s' from the selection of devices at the top left of xCode
  6. Checkpoint: Hit the run button on the top left of xCode - you should see a blank white screen
Add a textfield to your storyboard
  1. In the Project Navigator on the far left of xCode, select Main.storyboard
  2. Find the Object Library at the bottom of the Utilities panel (which is on the far right of xCode). If you don't see the utilities panel, click the Utilities button at the top right
  3. At the bottom is a search panel. Find or search for 'Text Field'
  4. Drag one to the furthest left guider on your View, about 1/3 from the top of your view
  5. Increase the width of the text field by dragging the right side of the text view to the rightmost guider
  6. Next, let's change the placeholder text
  7. In the Utilities Slider on the right side of Xcode, find the Attributes Inspector (4th icon on the top bar from the left)
  8. Enter 'new todo' next to 'Placeholder'
  9. Checkpoint: Hit the run button on the top left of xCode - you should see a blank white screen with a text field that says 'New Todo' (don't worry if the text field goes off of the screen)

Getting Our Views Together: The storyboard

Position text field using Auto Layout

When you run your code, you can change orientation by going to 'Hardware' and clicking one of the rotate buttons. Note that when you do so, your text field doesn't adjust its size automatically. Let's address that.

  1. In the Project Navigator on the far left of xCode, select Main.storyboard
  2. Click on the Text Field in the View
  3. Set top space on the top
  4. Control+Click and Drag from the Text Field to the area right above, which should highlight blue and say 'View'
  5. Release your drag
  6. Select 'Top Space to Top Layout Guide' from the small black menu
  7. Set leading space on the left
  8. Control+Click and Drag from the Text Field to the area left of the view, which should highlight blue and say 'View'
  9. Release your drag
  10. Select 'Leading Space to Container' from the small black menu
  11. Set Trailing space on the right
  12. Control+Click and Drag from the Text Field to the area right of the view, which should highlight blue and say 'View'
  13. Release your drag
  14. Select 'Trailing Space to Container' from the small black menu
  15. Checkpoint: Hit the run button on the top left of xCode
  16. You should see the Text Field with equal spacing on each side and spacing from the top bar
  17. On the top bar of your computer click 'Hardware' and click 'Rotate Left' to enter landscape view
  18. The spacing from the left, right, and top sides should match the portrait view
Create a table view controller
  1. In the Project Navigator on the far left of xCode, select Main.storyboard
  2. Drag a table view controller and drop it into the canvas. If you're lacking in space, right click on the canvas to zoom out <make sure you're zoomed in to 100%>
1. Click the arrow on the left of your view controller 1. Drag and drop it on your table view controller to set the table view controller as the initial scene 1. Checkpoint: Hit the run button on the top left of xCode - you should see an empty table view
Add a segue to navigate forward
  1. Select your Table View Controller
  2. With the view controller selected, choose Editor > Embed In > Navigation Controller
  3. In the canvas, select the newly created title area on your table view (or in the outline view select Navigation Item under Table View Controller)
  4. In the Attributes inspector, type 'My To-Do List' in the Title field
  5. Add a '+' button
  6. Drag a Bar Button Item object from the Object Library to the far right of the navigation bar in the table view controller
  7. Select the newly created bar button item
  8. Checkpoint: Hit the run button on the top left of xCode - you should see your newly created + button, but it won't do anything just yet 1. In the Attributes inspector, find the Identifier option in the Bar Button Item section. Choose 'Add' from the Identifier pop-up menu
  9. Checkpoint: Hit the run button on the top left of xCode - you should see your newly created + button, but it won't do anything just yet
Configure the Add button
  1. Control+Click and Drag from the Add button to the View Controller with the New Todo Text Field
  2. Release the drag
  3. Click 'present modally' from the small menu that appears
  4. Checkpoint: Hit the run button on the top left of xCode - Click the Add button and the New Todo view should appear, but there is no way to get back
Add a navigation controller to the view controller
  1. Select your view controller
1. With the view controller selected, choose Editor > Embed In > Navigation Controller 1. In the canvas, select the newly created title area on your table view (or in the outline view select Navigation Item under Table View Controller) 1. In the Attributes inspector, type 'Add Todo' in the Title field 1. Add a 'Done' button right to the title 1. Drag a Bar Button Item object from the Object Library to the far right of the navigation bar in the table view controller 1. Select the newly created bar button item 1. In the Attributes inspector, find the Identifier option in the Bar Button Item section. Choose 'Done' from the Identifier pop-up menu 1. Add a 'Cancel' button to the left of the title 1. Drag a Bar Button Item object from the Object Library to the far right of the navigation bar in the table view controller 1. Select the newly created bar button item 1. In the Attributes inspector, find the Identifier option in the Bar Button Item section. Choose 'Cancel' from the Identifier pop-up menu 1. Checkpoint: Hit the run button on the top left of xCode - you should see your newly created 'Done' and 'Cancel' buttons when you navigate to 'Add a todo', but they won't do anything just yet

Getting Our Hands Dirty: The swift

Create a custom view controller that is a subclass of UIViewController
  1. Choose File > New > File (or press Command-N)
  2. On the left of the dialog that appears, select the Cocoa Touch Class under iOS
  3. In the Class field, type 'AddTodoItemViewController'
  4. In the Language field, select Swift
  5. Choose UIViewController in the “Subclass of” pop-up menu
  6. Make sure the “Also create XIB file” option is unselected
  7. Click Next
  8. The save location will default to your project directory. Leave that as is
  9. The Group option will default to your app name, ToDoList. Leave that as is
  10. The Targets section will default to having your app selected and the tests for your app unselected. That’s perfect, so leave that as is
  11. Click Create

Now that you’ve created a custom view controller subclass, you need to tell your storyboard to use your custom class instead of the generic view controller.

  1. In the project navigator, select Main.storyboard
  2. Select your View Controller (in the Outline View, this is 'View Controller' under the 'Add a todo Scene')
  3. With the view controller selected, open the Identity inspector in the utility area.
  4. In the Identity inspector, open the pop-up menu next to the Class option
  5. Choose AddToDoItemViewController
Create a custom table view controller that is a subclass of UITableViewController
  1. Choose File > New > File (or press Command-N)
  2. On the left of the dialog that appears, select the Cocoa Touch Class under iOS
  3. In the Class field, type 'TodoListTableViewController'
  4. In the Language field, select Swift
  5. Choose UITableViewController in the “Subclass of” pop-up menu
  6. Make sure the “Also create XIB file” option is unselected
  7. Click Next
  8. The save location will default to your project directory. Leave that as is
  9. The Group option will default to your app name, ToDoList. Leave that as is
  10. The Targets section will default to having your app selected and the tests for your app unselected. That’s perfect, so leave that as is
  11. Click Create
  12. In the project navigator, select Main.storyboard
  13. Select your Table View Controller (in the Outline View, this is 'Table View Controller' under the 'Todo list Scene')
  14. With the table view controller selected, open the Identity inspector in the utility area.
  15. In the Identity inspector, open the pop-up menu next to the Class option
  16. Choose ToDoListTableViewController
Connecting cancel and done buttons to exit segue <reword?>

WHAT YOU SHOULD BE ABLE TO DO

  1. Navigate to your TodoListTableTableViewController.swift
  2. Add the following function (this registers the action and allows it to be used in storyboard):
@IBAction func unwindToList(segue: UIStoryboardSegue) {
    println("Unwinding")
}

  1. Navigate to your storyboard
  2. On the canvas, Control-drag from the 'Cancel' button to the Exit item right above (the right item of the three squares above )
  3. Choose unwindToList: from the shortcut menu
  4. Do the same for the 'Done' button
  5. Checkpoint: Hit the run button on the top left of xCode - when you navigate over to Add a todo, your cancel and done buttons should pop you back over to Todo list (your table view)

WHAT YOU HAVE TO DO BECAUSE XCODE 6 IS BROKEN

(Note: see this stack overflow question for more details on this bug)

  1. Choose File > New > File (or press Command-N)
  2. On the left of the dialog that appears, select the Header File under iOS
  3. Save as 'TodoListTableViewController'
  4. Add the following to TodoListTableViewController.h:
@interface TodoListTableViewController

-(IBAction) unwindToList:(UIStoryboardSegue *) segue;

@end

This registers the action and allows it to be used in storyboard

  1. Navigate to TodoListTableViewController.swift
  2. Change the class definition to:
@objc(TodoListTableViewController) class TodoListTableViewController: UITableViewController {

This effectively makes it a C# class, which sucks, but there you go

  1. Add the Unwind function:
func unwindToList(segue: UIStoryboardSegue) {

}
  1. Re-register TodoListTableViewController, since we mucked about with it
  2. Go to your storyboard
  3. Click your table view controller
  4. Go to the Identity Inspector
  5. Pull down the dropdown and re-select TodoListTableViewController
  6. On the canvas, Control-drag from the 'Cancel' button to the Exit item right above (the right item of the three squares above )
  7. Choose unwindToList: from the shortcut menu
  8. Do the same for the 'Done' button
  9. Checkpoint: Hit the run button on the top left of xCode - when you navigate over to Add a todo, your cancel and done buttons should pop you back over to Todo list (your table view)
Create a Data Class
  1. Choose File > New > File
  2. In the iOS section on the left select Source
  3. Select Cocoa Touch Class and click Next
  4. Type ToDoItem in the Class field
  5. In the Subclass dropdown select NSObject
  6. Make sure Language is set to Swift and select Next
  7. Click Create Your class should look like this
import UIKit

class ToDoItem: NSObject {
   
}
Code a Data Class called TodoItem
  1. Give it a name and completed variable and initialize them like so:
import UIKit

class TodoItem: NSObject {
    var itemName:String = ""
    var completed:Bool = false

    init(itemName:String) {
        self.itemName = itemName
    }
}
Give your table view controller an array of TodoItems
  1. Navigate to your TodoTableListViewController
  2. Give it an array of TodoItems:
var todoItems: [TodoItem] = []

  1. Give it an loadInitialData function that populates your array:
func loadInitialData() {
    todoItems = [
        TodoItem(itemName: "Go to the dentist"),
        TodoItem(itemName: "Fetch groceries"),
        TodoItem(itemName: "Sleep")
    ]
}

  1. Load your initial data from the viewDidLoad function:
override func viewDidLoad() {
    super.viewDidLoad()
    loadInitialData()
}

  1. Make the number of sections in your table one:
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    return 1
}

  1. Next, let's create a function that returns the number of rows per section. Since we only have one section, we'll return a count of the todoItems. Add the following function:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return todoItems.count
}

<this function looks retarded because: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_202 (go to External Parameter Names>

  1. The last function we need will genereate UITableViewCells for each row at a specific index
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("ListPrototypeCell") as UITableViewCell
    var todoItem = todoItems[indexPath.row]
    cell.textLabel.text = todoItem.itemName
    
    return cell
}
  1. In the project navigator select Main.storyboard
  2. In the Todo list TableView select the Prototype Table View Cell
  3. In the Utilities slider on the right, go to the Attributes Inspector (4th icon from the left)
  4. For the Identifier change the Reuse Identifier to ListPrototypeCell <note: don't copy paste - type slowly and click out of box to make sure storyboard updates>
  5. Checkpoint - Run the app, the Table View should populate with the initial data "Go to the dentist", "Fetch groceries", "Sleep"
Mark item as complete
  1. In the project navigator select TodoListTableViewController.swift
  2. Create the override function tablView didSelectRowAtIndexPath
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    
    var tappedItem = todoItems[indexPath.row] as TodoItem
    tappedItem.completed = !tappedItem.completed
    
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}
  1. Add code to tableView cellForRowAtIndexPath to display checkmark on completed items
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("ListPrototypeCell") as UITableViewCell
    var todoItem = todoItems[indexPath.row]
    cell.textLabel.text = todoItem.itemName
    
    if (todoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryType.None;
    }
    
    return cell
}
  1. Checkpoint - run the app, click an item and a check mark should appear next to it, click it again to make the checkmark disappear
Lastly - add new items
  1. Head on over to your Storyboard
  2. In the outline view, select the AddTodoItemViewController object
  3. Click the Assistant button in the upper right of the window’s toolbar to open the assistant editor (the suit-looking icon)
  4. The editor on the right should appear with AddTodoItemViewController.swift displayed. If it isn’t displayed, click the filename in the editor on the right and choose AddTodoItemViewController.swift
  5. Select the text field in your storyboard
  6. Control-drag from the text field on your canvas to the code display in the editor on the right, stopping the drag at the line just below the class AddTodoItemViewController: UIViewController { line
  7. In the dialog that appears, for Name, type textField and leave the rest of the options as they are
  8. Click Connect
  9. This will hook up your button to your code and create a connection in the storyboard xml for you. In your code it should look like @IBOutlet var textField: UITextField
  10. Do the same for the Done button, calling it 'doneButton'. In your code it should look like @IBOutlet var doneButton: UIBarButtonItem
  11. Next, let's give our controller a todoItem that it will store our 'add' data into: var todoItem: TodoItem = TodoItem(itemName: "")
  12. Of course we also need to be able to take data from the user input and assign it to this variable:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
    if (self.textField.text.utf16count > 0) {
        self.todoItem = TodoItem(itemName: self.textField.text)
    }
}

  1. Lastly, we need to head back over to TodoListTableTableViewController.swift and update our unwindToList to take the data that AddTodoItemViewController.swift is holding and pop it into the array of todoItems:
func unwindToList(segue: UIStoryboardSegue) {
    var source = segue.sourceViewController as AddTodoItemViewController
    var todoItem:TodoItem = source.todoItem
    
    if todoItem != nil {
        self.todoItems += todoItem
        self.tableView.reloadData()
    }
}
  1. Checkpoint: Run your app - you should be able to add items!

That's it!

As mentioned before, we love feedback - please open issues about anything you feel is missing, or email the authors directly about questions or feedback at [email protected].

git-swift-todo-tutorial's People

Contributors

jeanbza avatar marcusmolchany avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.