LoginSignup
15
12

More than 5 years have passed since last update.

WatchKitでPageControlを表示する

Last updated at Posted at 2014-12-10

この投稿は Apple Watch Advent Calendar 2014 の 11日目の記事です。

pagecontrol.png

上のスクショのように 画面を横にスワイプして簡単に切り替えられるApple Watchアプリ を作ろうとして、PageControllに相当するクラスを探していたのですが、

  • WKInterfaceButton
  • WKInterfaceDate
  • WKInterfaceGroup
  • WKInterfaceImage
  • WKInterfaceLabel
  • WKInterfaceMap
  • WKInterfaceSeparator
  • WKInterfaceSlider
  • WKInterfaceSwitch
  • WKInterfaceTable
  • WKInterfaceTimer

・・・あれ?それらしきものがない!!

というわけで、Appleが公開しているサンプルコードWatchKit Catalogの中にそれっぽいことをしている個所がないか探してみることに。

そしたら、ありました!

どうやっていたか

presentControllerWithNames:contexts: というメソッドを使ってました。

Appleのドキュメントに

Presents a page-based interface modally.

とあるように、pageベースの画面をモーダル表示してくれるようです。

第1引数に 表示したい画面(WKInterfaceController)の Identifier を配列で渡すことにより、自動的にPageControlを作ってくれます。

NSArray *controllerNames = @[@"pageController", @"pageController", @"pageController", @"pageController", @"pageController"];
[self presentControllerWithNames:controllerNames contexts:nil];

上の例では PageController という Identifier を持った画面を5つ並べた画面をモーダル表示します。(スクリーンショットを載せたいのですが、NDAに違反する可能性があるのでやめておきます)

疑問点

モーダルでPageControlを表示できるのはわかったんですが、初期画面でPageControlを表示する方法がわかってません。できればモーダルを挟まずにPageControlを表示したいのですが、その方法を知っている方がいらっしゃいましたらぜひ教えて下さい!

追記:
初期画面でPageControlを表示する方法をコメント欄で教えて頂きました!
@fmtonakai さん、ありがとうございました!

追記2:
Xcode6.2 beta2で reloadRootControllersWithNames:contexts: というメソッドが追加されており、そちらを使うことで初期画面でもPageControlを表示できるようです。

15
12
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12