Jagat Dave Jagat Dave - 1 month ago 10
iOS Question

Pass array data from one interface to another in watchkit using pagebased navigation?

I am working on Apple watch. I am tried to pass the array data from one

InterfaceController
to another using
PageBasedNavigation
in
watch-kit
. So can any one suggest me how to pass the array while swipe form left in
watch-kit
programmatically ?

For Creating page-based interface i follow the following steps:


  1. In my storyboard, add interface controllers for each of the pages in your interface.

  2. Control-click your app’s main interface controller and drag to another interface controller.

  3. The second interface controller should highlight, indicating that a segue is possible.

  4. Release the mouse button.

  5. Select next page from the relationship segue panel.



Using the same technique, create segues from each interface controller to the next.

Thanks in advance.

Answer

The WatchKit method for passing objects to a page-based WKInterfaceController is different than in iOS. While you will see a relationship segue on the Storyboard, when you click on it there is no option to name this segue (which is the first step to using the prepareForSegue: method in iOS).

Rather, what you do is pass one array that contains "context" objects, one of which will be provided to each WKViewController that is managing a page. In Objective-C:

+ (void)reloadRootControllersWithNames:(NSArray *)names
                          contexts:(NSArray *)contexts

In Swift:

class func reloadRootControllersWithNames(_ names: [AnyObject]!,
                             contexts contexts: [AnyObject]!)

If you have a object you want to pass from one to the other, you set that object as the context for each page:

NSArray * namesArray = @[@"Page 1", @"Page 2", @"Page 3"];
NSArray * contextsArray = @[myObject, myObject, myObject];
[self reloadRootControllersWithNames:namesArray contexts:contextsArray];

Somewhat counter-intuitively from the method name, while this method is called reloadRootControllersWithNames:, the WatchKit documentation on Managing Page-Based Navigation indicates that this same method should be used to seed these values at launch time, as well as any time you want to reload this data at runtime.