11

Is there any way dynamically create pages in a page-based navigation? In every example I read, the pages were created as Interface Controllers and linked in the Storyboard.

Floern
  • 33,559
  • 24
  • 104
  • 119
Dănuț Mihai Florian
  • 3,075
  • 3
  • 26
  • 44

3 Answers3

11

Here is the way to do it

WKInterfaceController.reloadRootControllersWithNames(["pageController", "pageController"], contexts: ["pageController", "pageController"])
mxb
  • 3,300
  • 4
  • 20
  • 25
Dănuț Mihai Florian
  • 3,075
  • 3
  • 26
  • 44
  • this creates a double spinning animation though, once to load the initial controller, and then again to reload with the new pages. – Erich Feb 04 '15 at 21:43
  • 2
    Where did you place it? I also get an infinite recursion of pages – edwardmp Feb 15 '15 at 16:53
  • @Erich i agree, but i didn't find anything better at the moment. @edwardmp I placed this code in the default ViewController of the watch interface in the viewDidLoad method. The arrays that i pass as parameters to the `reloadRootControllersWithNames` method should be dynamically generated. I used those just as examples to illustrate the solution. – Dănuț Mihai Florian Feb 16 '15 at 00:34
  • use a static variable to avoid infinite recursion. – Gamma-Point Apr 11 '15 at 06:02
  • Please delete the original solution posted and keep only the update. – Thiru Apr 16 '15 at 09:45
  • I use two `WKInterfaceController`. Call `WKInterfaceController.reloadRootControllersWithNames` from `awakeWithContext` of Initial controller. – Min Soe Apr 29 '15 at 07:31
  • 1
    The back button from the status bar goes missing in this case. How do we navigate back to any previous InterfaceController then? – MixCoded Jul 15 '15 at 05:06
5

To avoid infinite loop use:

static BOOL first = YES;
- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    if (first) {
        [WKInterfaceController reloadRootControllersWithNames:[NSArray arrayWithObjects:@"SinglePageICIdentifier",@"SinglePageICIdentifier", nil] contexts:[NSArray arrayWithObjects:@"First",@"Second", nil]];
        first = NO;
    }

}
Gamma-Point
  • 1,514
  • 13
  • 14
-1

I presumed you want to display several page of the same kind of data.

Apple Watch Programming Guide states the following:

This style is suited for apps with simple data models where the data on each page is not closely related to the data on any other page.

Therefore, I think you should stick to table views to display several items that are closely related (by kind) to each other. From my point of view, page-based controller navigation (swipe) is too slow / boring to be used for lots of pages. Also, I think it could take a lot of time to load the page-based controller.

ramdam
  • 9
  • 1