How to pass data via multiple segue

 

This tutorial has been updated for X code 7.3 & swift 2.2

This is most commonly used when you have to add item or row to uitableview. Sometimes you just add single string input to new row or sometimes you have multiple objects to add to tableview row. In todays tutorial I am going to cover both with example of grocery list app.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to Grocery List demo & save it on desktop.

new x code project

 

2. Go to main storyboard & select view controller & delete it. Add tableview controller in main storyboard.

add table view controller

 

3. Select tableview controller & make it initial view controller

set initial view controller

 

4. Select prototype cell & set its identifier to tableCell

 

 

prototype cell identifier

5. Now delete view controller swift file & add new cocoa touch class file of class UITableViewController

new table view controller swift file

 

6. Go to main storyboard; select table view controller & set its class to GroceryListTableViewController

set custom class

 

7. Go to grocery list table view controller swift file & create array to store grocery list

class GroceryListTableViewController: UITableViewController {
      var groceryList = [String]()

8. Set number of sections to 1 & number of rows per section to groceryList.count

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return groceryList.count
}

9. Uncomment cell for row at index path & add following code to display the list

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)

    // Configure the cell...
    cell.textLabel?.text = groceryList[indexPath.row]

    return cell
}

10. Go to main storyboard; select table view controller & click on editor -> embed in -> Navigation Controller & set its title to Grocery List

set navigation title

 

11. Add bar button items & set its identifier to Add

add bar button item

 

12. Add another tableview controller to storyboard

add another tableview controller

 

13. Select new table view controller & click on editor -> embed in -> Navigation Controller & set its title to Add new item

add navigation controller

 

14. Add one bar button item on both sides of title & set their name to Save & Cancel

add two bar button item

 

15. Now select the tableview & set its content to Static cells, its sections to 1 & style to Grouped

set static cells

 

16. Set rows to 1 & header to Item name

set section header

 

17. Add textfield to the row

add textfield

 

18. Select text field and set its font size to System 17 & its border to No boarder

set font for textfield

 

19. Connect add bar button to new navigation controller via modal segue

set modal segue

20. Create new cocoa touch class file of class UITableViewController

add new tableview controller

 

21. Go to main storyboard; select table view controller & set its class to NewItemTableViewController

set custom class

 

22. Create a variable to store new grocery item name

class NewItemTableViewController: UITableViewController {
        var itemName: String?

23. Comment out number of section method & number of rows per section method

/*
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0
}
*/

24. Connect itemNameTextField as outlet to swift file

textfield to swift file

 

25. Add did select row at index path method to start typing in textfield when anywhere in row is tapped

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 0 && indexPath.row == 0 {
        itemNameTextField.becomeFirstResponder()
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

26. Go to grocery list table view controller swift file and add following methods for cancel unwind segue & save unwind segue

@IBAction func cancelToGroceryList (segue:UIStoryboardSegue) {

}

@IBAction func saveToGroceryList (segue: UIStoryboardSegue) {

}

27. Go to main storyboard and create cancel unwind segue & save unwind segue

set unwind segue

28. Set save to grocery list unwind segue identifier to SaveItem

save unwind segue identifier

 

29. Set cancel to grocery list unwind segue identifier to CancelToList

cancel unwind segue identifier

 

30. Uncomment prepare for segue in NewItemTableViewController & add textfield text to new item

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SaveItem" {
        itemName = itemNameTextField.text
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

31. Go to grocery list table view controller and add this string to list array & update table

@IBAction func saveToGroceryList (segue: UIStoryboardSegue) {

    let newItemViewController = segue.sourceViewController as! NewItemTableViewController

    let name = newItemViewController.itemName

    if name == "" {

    }
    else {
        groceryList.append(name!)

        let indexToInsert = groceryList.count == 0 ? 0 : groceryList.count - 1

        let indexPath = NSIndexPath(forRow: indexToInsert, inSection: 0)

        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }

}

Here short hand for if else loop is used to calculate index to insert.
32. Now build and run app in iPhone 5s Add new item to grocery list.

pass data via segue
Download X code file
Challenge 1: Add category list to grocery list

challenge 1 solution

Challenge 2: Add new category to category list

challenge 2 solution
Download Challenge Solution

Total 5 Votes
0

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?