iOS 8 HealthKit Santiapps Marcio Valenzuela

HealthKit for iOS8: Part 4

iOS 8 HealthKit Santiapps Marcio Valenzuela
iOS 8 HealthKit

Ok so the Journal view controller was quite simple.  Let’s take another quick break by looking at an even simpler view controller, the FoodPicker:

import Foundation

import UIKit

import HealthKit

class FoodPickerViewController: UITableViewController {

var selectedFoodItem: FoodItem?

let FoodPickerViewControllerTableViewCellIdentifier:NSString = "cell"

let FoodPickerViewControllerUnwindSegueIdentifier:NSString = "FoodPickerViewControllerUnwindSegueIdentifier"

var foodItems: NSArray = NSArray()

var energyFormatter: NSEnergyFormatter {

var energyFormatter: NSEnergyFormatter?

var onceToken: dispatch_once_t = 0

dispatch_once(&onceToken, {

energyFormatter = NSEnergyFormatter()

energyFormatter?.unitStyle = NSFormattingUnitStyle.Long

energyFormatter?.forFoodEnergyUse = true

energyFormatter?.numberFormatter.maximumFractionDigits = 2

})

return energyFormatter!

}

var  healthStore:HKHealthStore?

}

Here we are looking at imports, subclassing UITableViewController and property declarations.

Our first property is selectedFoodItem, which will be set to whatever value the user selects from this tableview as an item that he or she consumed.  Then we have 2 identifiers, one for our tableviewcell and another for our unwind segue.  Remember to set both in the storyboard.  To set the cell identifier you must select the cell in the tableview and for the unwind segue you must select the segue from the “Add” button in Journal to the FoodPicker.  Then we create an array to store the hard coded values of our FoodItems.  Ideally you might want to pull this data from a web service.

Notice we use the NSEnergyFormatter here again.  You could also just create a class for this and access it globally.  Finally we add our health store variable.

Now let’s look at our viewDidLoad method:

override func viewDidLoad() {

super.viewDidLoad()

self.foodItems = [FoodItem(name: "Wheat Bagel", joules:240000.0),

FoodItem(name: "Bran with Raisins", joules:190000.0),

FoodItem(name: "Regular Instant Coffee", joules:1000.0),

FoodItem(name: "Banana", joules:439320.0),

FoodItem(name: "Cranberry Bagel", joules:416000.0),

FoodItem(name: "Oatmeal", joules:150000.0),

FoodItem(name: "Fruits Salad", joules:60000.0),

FoodItem(name: "Fried Sea Bass", joules:200000.0),

FoodItem(name: "Chips", joules:190000.0),

FoodItem(name: "Chicken Taco", joules:170000.0) ]

}

Nothing special here, just hardcoding some FoodItems into our array.  So now our tableview methods:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.foodItems.count

}

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {

let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(FoodPickerViewControllerTableViewCellIdentifier, forIndexPath:indexPath!) as UITableViewCell

var foodItem: FoodItem = self.foodItems[indexPath!.row] as FoodItem

cell.textLabel.text = foodItem.name

cell.detailTextLabel?.text = energyFormatter.stringFromJoules(foodItem.joules)

return cell;

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

NSLog("performing unwind segue from FOODPICKER!")

if (segue.identifier == FoodPickerViewControllerUnwindSegueIdentifier) {

var indexPathForSelectedRow: NSIndexPath = self.tableView.indexPathForSelectedRow()!

self.selectedFoodItem = self.foodItems[indexPathForSelectedRow.row] as? FoodItem

}

}

There, now that’s a nice breather.   We set our rows according to the number of items in the array, set our cell values in the same way we did before.  And in our prepareForSegue method, we check our identifier and then set our own selectedFoodItem property from whatever the user selected.

Ok so you’ve had 2 easy classes to rest.  Now let’s get into the longest one.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s