Learn watchOS Programming!

27
Learn watchOS Programming!

Transcript of Learn watchOS Programming!

Page 1: Learn watchOS Programming!

Learn watchOS

Programming!

Page 2: Learn watchOS Programming!

Communication ChannelGoogle Document for live chat - http://tiny.cc/watchOSTechTalk

Github link for the project - https://github.com/patilsnehal/watchOSTableview

Page 3: Learn watchOS Programming!

Create watchOS Application with tableview

Page 4: Learn watchOS Programming!

Create Project

Page 5: Learn watchOS Programming!

Add tableview

Page 6: Learn watchOS Programming!

Change the color of the global tint

Page 7: Learn watchOS Programming!

Change Identifier

Page 8: Learn watchOS Programming!

Add Group and then labels to it

Page 9: Learn watchOS Programming!

Change font, alignment etc

Page 10: Learn watchOS Programming!

Add a watchKit class

Page 11: Learn watchOS Programming!

ScheduleInterfaceController

Page 12: Learn watchOS Programming!

Change the class type as ScheduleInterfaceController

Page 13: Learn watchOS Programming!

Lets do some coding finally :)

Connect table @IBOutlet var WWCMeetupTable: WKInterfaceTable!

Set up Number of rows WWCMeetupTable.setNumberOfRows(10, withRowType: "WWCRow");

Page 14: Learn watchOS Programming!

Hit Run !

Page 15: Learn watchOS Programming!

MeetupRowController

Page 16: Learn watchOS Programming!

Change the class type as MeetupRowController

Page 17: Learn watchOS Programming!

Add meetup.json and meetup.swift (Shared folder) [ { "startTime": "10:00 AM", "endTime": "12:00 PM", "title": "Python Meetup", }, { "startTime": "12:00 PM", “endTime": "2:00 PM", "title": "iOS Meetup", }, { "startTime": "2:00 PM", "endTime": "4:00 PM", "title": "WatchOS Meetup", }, { "startTime": "4:00 PM", "endTime": "6:00 PM", "title": "Android Meetup", }, { "startTime": "6:00 PM", "endTime": "8:00 PM", "title": "Java Meetup", }]

class Meetup { let title: String let starttime: String let endtime: String class func allMeetups() -> [Meetup] {

var meetups = [Meetup]()if let path = NSBundle.mainBundle().pathForResource("Meetup", ofType: "json"),

let data = NSData(contentsOfFile: path) { do { let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [Dictionary<String, String>] for dict in json { let meetup = Meetup(dictionary: dict) meetups.append(meetup) } } catch { print(error) }

}return meetups

} init( title: String, starttime: String, endtime: String) {

self.title = titleself.starttime = starttimeself.endtime = endtime

} convenience init(dictionary: [String: String]) {

let title = dictionary["title"]!let starttime = dictionary["startTime"]!let endtime = dictionary["endTime"]!self.init(title: title, starttime: starttime, endtime: endtime)

}}

Page 18: Learn watchOS Programming!

Add files and add them to app and extension target

Page 19: Learn watchOS Programming!

Configure the labels on MeetupRowControllerclass MeetupRowController: NSObject {

@IBOutlet var timeLabel: WKInterfaceLabel!@IBOutlet var titleLabel: WKInterfaceLabel!

// 1var meetup: Meetup? {

// 2 Add a property observer that is triggered whenever the property is set; didSet {

// 3 Check of meetup is not nil if let meetup = meetup {

// 4 configure the labels using the relevant properties of meetup timeLabel.setText(meetup.starttime) titleLabel.setText(meetup.title) } } }}

Page 20: Learn watchOS Programming!

ScheduleInterfaceControllervar meetups = Meetup.allMeetups()

override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) // Set up a Row count WWCMeetupTable.setNumberOfRows(meetups.count, withRowType: "WWCRow"); // Add data for each row for index in 0..<WWCMeetupTable.numberOfRows { if let controller = WWCMeetupTable.rowControllerAtIndex(index) as? MeetupRowController { controller.meetup = meetups[index] } }}

Page 21: Learn watchOS Programming!

Hit Run!

Page 22: Learn watchOS Programming!

Add new Interface controller and add design it.● Change Identifier and name it

as “Meetup”● Copy a image called static

maps into the project and assign it to the image.

● Create a group and add 2 buttons to it - YES & NO

Page 23: Learn watchOS Programming!

MeetupInterfaceControllerclass MeetupInterfaceController: WKInterfaceController {

@IBOutlet var timeLabel: WKInterfaceLabel!@IBOutlet var titleLabel: WKInterfaceLabel!

// 1

var meetup: Meetup? {

// 2 didSet {

// 3 if let meetup = meetup {

// 4 timeLabel.setText("\(meetup.starttime) to \(meetup.endtime)") titleLabel.setText(meetup.title) } }

}

override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) if let meetup = context as? Meetup { self.meetup = meetup }

}

}

Page 24: Learn watchOS Programming!

ScheduleInterfaceControllerAdd a method didSelectRowAtIndex to push a viewcontroller

override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) { let meetup = meetups[rowIndex] presentControllerWithName("Meetup", context: meetup)}

Page 25: Learn watchOS Programming!

Hit Run!

Page 26: Learn watchOS Programming!
Page 27: Learn watchOS Programming!