はじめての 
WKInterfaceController 
2014/11/25 @TachibanaKaoru
About Me 
• @TachibanaKaoru 
• 渋谷のgenesix (Voyage Group) で働くiOSエンジ 
ニアです 
• Blog : http://www.toyship.org/
前提 
• この資料はAppleから一般に公開されている資料 
に基づいたものです 
• 2014/11/25現在の状況に基づいており、今後変 
更される可能性もあります。
Reference 
• WatchKit Programming Guide 
• https://developer.apple.com/library/prerelease/ios/documentation/General/ 
Conceptual/WatchKitProgrammingGuide/index.html 
• WatchKit Framework Reference 
• https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/ 
Reference/WatchKit_framework/index.html 
• Apple Watch Human Interface Guidelines 
• https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/ 
Conceptual/WatchHumanInterfaceGuidelines/index.html 
• Getting Started with WatchKit 
• http://devstreaming.apple.com/videos/watch/Getting_Started_With_Watchkit/ 
Getting_Started_With_Watchkit.m3u8
WatchKit 
• Apple Watchのアプリのため 
のフレームワーク 
• 2014/11/18 発表
Watch App Interface 
WatchKit App Notification Glance
WatchKit App 
WatchKit App Notification Glance
WatchKit App 
• 通常のアプリ画面 
• ユーザー入力を受け付け、複数の画面を遷移す 
ることが可能。
Notification 
WatchKit App Notification Glance
Notification 
• オプショナル 
• Notificationに対応するための画面。 
• iOS8から導入された選択肢付きのNotificationに 
対応。
Glance 
WatchKit App Notification Glance
Glance 
• オプショナル 
• 文字盤を上に向かってスワイプする 
• ユーザー入力は基本的には不可能
WKInterfaceController 
• Apple Watchにおいて、一画面の描画・コント 
ロールを司るクラス 
• iOSアプリにおけるUIViewController 
• このクラスを継承して、それぞれの画面のクラス 
を作成し、その上にUIパーツを配置します。
UIパーツ 
• WKInterfaceControllerにのせられる画面パーツは 
限られており、独自UIなどは使うことができま 
せん。
Label & Button 
• WKInterfaceLabel 
• WKInterfaceButton
Slider & Switch 
• WKInterfaceSlider 
• ボタン 
• WKInterfaceSwitch 
• on/off Switch
Image & Map 
• WKInterfaceImage 
• WKInterfaceMap 
• マップを表示するだけ
Table & Timer Label 
• WKInterfaceTable 
• WKInterfaceTimerLabel
その他 
• WKInterfaceGroup 
• 複数の画面パーツのグルーピングを行う 
• WKInterfaceSeparator 
• 単なるセパレーター
Menu 
• 画面を長押しすると表示 
されるコンテキストメ 
ニュー 
• WKInterfaceControllerご 
とに0~4個の範囲で設定 
することができる。
Menu 
• それぞれのボタンが押された際の動作はTarget- 
Actionで設定可。 
• システムで用意されたアイコンもあるが、自分で 
画像を指定することも可能。 
• ボタンサイズや位置の変更は不可。
Menu
画面レイアウト 
• それぞれのUIパーツの幅と高さは変更可能 
• Relative to Container (親アイテムに対する比 
率) 
• Size to fit Content (親アイテムをFillする) 
• Fixed Width (ピクセル指定)
画面レイアウト 
• それぞれのUIパーツの位置をCGPointで指定する 
ことはできません。 
• 左上から順番に並べられます。
Watch Appの画面遷移 
• Modal 
• Single Modal 
• Paged Modal 
• Hierarchical (Navigation) 
• Page-based
Modal View 
• 左上にタイトルが表示され、自動的に「Close」 
機能を持つボタンとなる。 
• 1枚表示のControllerの表示または複数枚の 
Controllerの表示が可能。
Single Mordal 
• 一時的なタスク処理 
• 下から上へのアニメー 
ション
Paged Mordal 
• 一時的なタスク処理 
• 下から上へのアニメー 
ション 
• 複数ページはSwipeで遷 
移
Hierarchical 
• iOSのNavitaion Controller 
• 子Controllerを一つ指定し、Pushする
Hierarchical 
• 左上にタイトルが表示され、自動的に「Close」 
機能を持つボタンとなる。
Page-based 
• iOSのUIPageViewController 
• ただし、ページ数やページインスタンスは生成 
時に決定。動的な変更には対応しない
Page-based 
• 左右Swipeでページ移動
HierarchicalとPage-based 
• HierarchicalとPage-based のページ遷移の混在 
は禁止されています。
Controller間のデータ受け渡し 
• 今までのiOSアプリではObjectを指定して行って 
いました。 
UIViewController* myViewController = [[UIViewController alloc] init]; 
myViewController.friends = friendsarray; 
データを 
渡したい
Controller間のデータ受け渡し 
• ところが、WKInterfaceControllerではその方法が 
使えません。 
• 例えば pushで新しいWKInterfaceControllerを生 
成する場合、pushはStoryboardのidentifierを指 
定して行います。 
[self pushControllerWithName:@"placeController" context:@"Shibuya"];
Controller間のデータ受け渡し 
• 一応 alloc + initでオブジェクトを生成することも 
可能ですが、これをpushする方法がありませ 
ん。 
LocalController* mycontroller = 
[[LocalController alloc] initWithContext:@"Shibuya"]; 
• →オブジェクトを指定してメッセージが送れな 
い。
Controller間のデータ受け渡し 
• そこで、context引数に、オブジェクトを渡すこ 
とでデータの受け渡しをおこなう。 
[self pushControllerWithName:@"placeController" context:@"Shibuya"]; 
• NSString、NSArray、NSDictionaryなど任意の 
クラスが指定可能
Controller間のデータ受け渡し 
• 受け側のWKInterfaceControllerのinitWithContext 
で引数の処理を行います 
- (instancetype)initWithContext:(id)context { 
self = [super initWithContext:context]; 
if (self) { 
  NSDictionary* myDict = context; 
NSString* name = [myDict valueForKey:@"name"]; 
[self.centerLabel setText:[NSString stringWithFormat:@"Name:%@", name]]; 
} 
return self; 
}
生成 
• Modalの場合も同様です。 
• Single Modal 
[self presentControllerWithName:@"peopleController" context:@"Alice"]; 
• Paged Modal 
NSArray *controllerPersons = @[@"peopleController", 
@"companyController", 
@"placeController"]; 
NSArray* contexts = @[ @{@"name":@"Alice"}, 
@{@"tel":@"03-1234-5678"}, 
@"Osaka"]; 
[self presentControllerWithNames:controllerNames contexts:contexts];
WatchKitのこれから 
• WatchKitは、制限も、がっかりポイントも多 
い、まだまだ未成熟なフレームワークです。 
• 積極的にApple への Feature Requestをしましょ 
う!

はじめてのWKInterfaceController