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:
In my storyboard, add interface controllers for each of the pages in your interface.
Control-click your app’s main interface controller and drag to another interface controller.
The second interface controller should highlight, indicating that a segue is possible.
Release the mouse button.
Select next page from the relationship segue panel.
Using the same technique, create segues from each interface controller to the next.
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.
I'm not working with Object-C anymore but I think object C is similar. i'm working with swift now. With Swift, you can create a new singleTon.swift file and put this inside of that:
//
// singleTon.swift
// testSingleTon
//
// Created by Standard on 1/14/15.
// Copyright (c) 2015 Standard. All rights reserved.
//
import UIKit
class singleTon: NSObject {
class var sharedInstance : singleTon {
struct Example {
static let instance = singleTon()
}
return Example.instance
}
var number = 0
var array = []
}
After that you can share data to all interface of your application. For example you can change the data inside first InterfaceController like this:
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
NSLog("%@ will activate", self)
singleTon.sharedInstance.array = ["new"]
}
and you can change or take the shareInstance in SecondInterface like this:
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
NSLog("%@ will activate", self)
println("share data: \(singleTon.sharedInstance.array)") // It's will show up the ["new"] here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With