How to pass selection via segue

 

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

Sometimes when saving settings or taking user input; you want user to select one item from list. Especially if you are frequently adding something. You will save time typing & just select appropriate option with one click. It also limits types of input to a list. So you don’t have to verify every time if input is valid or not. Today I am explaining this with example of selecting expense category in expense tracker app.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to expense category 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 Table View & set content to Static cells, section to 1 & style to Grouped

set static cells

 

 

5. Select table view section & set rows to 1

static table row

 

6. Select table view cell & set style to Right detail

cell style to right detail

 

7. Set its title to Category

set label to category

 

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

add navigation controller

 

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

table view controller swift file

 

10. Go to main storyboard; select table view controller & set its class to AddExpenseTableViewController

set custom class

 

11. Connect detail label as outlet to swift file

detail label to swift file

 

12. Uncomment number of section & number rows in 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
}

*/

13. Go to main storyboard and add another tableview controller

add another table view controller

 

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

add navigation controller

 

15. Control drag add expense table cell to new navigation controller & select show segue

show segue

16. Select prototype cell of new table view controller & set it to category

prototype cell identifier

 

17. Add new cocoa touch class file of class UITableViewController

add another table view controller swift file

 

18. Go to main storyboard; select table view controller & set its class to SelectCategoryTableViewController

set custom class

 

19. Create array to save all categories and fill in following categories

class SelectCategoryTableViewController: UITableViewController {
var categoryList = ["Rent", "Grocery", "Eating Out", "Cell Phone", "Internet", "Gas", "Insurance", "Electricity"]

20. Set number of section to 1 & number of rows per section to categoryList.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 categoryList.count
}

21. Uncomment cell for row at index path method and add following code

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

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

    return cell
}

22. Go to add expense swift file & add following code for unwind segue

@IBAction func selectedCategory (segue: UIStoryboardSegue) {

}

23. Go to main storyboard and create unwind segue

unwind segue

24. Select the unwind segue & set its identifier to pickedCategory

unwind segue identifier

25. Go to select category swift file and create new variable to save selected category

var pickedCategory: String?

26. Uncomment prepare for segue method & set selected category to new variable

// 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 == "pickedCategory" {
        let cell = sender as! UITableViewCell
        let index = tableView.indexPathForCell(cell)
        if let indexPath = index?.row {
            pickedCategory = categoryList[indexPath]
        }
    }

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

27. Go to add expense swift file & add following code to selected category method to display it on detail label

@IBAction func selectedCategory (segue: UIStoryboardSegue) {
    let selectedCategoryViewController = segue.sourceViewController as! SelectCategoryTableViewController

    if let selectedCategory = selectedCategoryViewController.pickedCategory {
        detailLabel.text = selectedCategory
    }
}

28. Now build and run app in iPhone 5s & select category

select category
Download X code file
Challenge 1: Set checkmark to selected category & save selection permanent

checkmark solution
Download Challenge 1 Solution
Challenge 2: Add new category to the list

add category solution
Download Challenge 2 Solution

Total 9 Votes
0

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?