我WatchConnectivity
用来尝试将NSManagedObject
调用类型的数据发送arrayOfOjects
到手表。每个object
都有一个名为的字符串属性title
。
Watch 上的InterfaceController
加载并显示并清空表 - 正如预期的那样,因为数组为空,然后当用户请求数据时,它使用didReceiveMessage
手机上的方法发送。
我不确定如何将字典数组添加到 objectsArray 以显示在WKInterfaceTable
.
有谁知道我如何将数据发送到手表以显示在表格中以进行更改并将它们与手机同步?
苹果手表:
class ObjectsInterfaceController: WKInterfaceController, WCSessionDelegate {
var session : WCSession!
var objectsArray = [[AnyObject]]()
@IBOutlet var table: WKInterfaceTable!
@IBOutlet var titleLabel: WKInterfaceLabel!
func loadTableData() {
table.setNumberOfRows(self.objectsArray.count, withRowType: "CellRow")
if self.objectsArray.count > 0 {
for (index, name) in self.objectsArray.enumerate() {
let row = self.table.rowControllerAtIndex(index) as! CellRowController
row.objectCellLabel.setText(name.title)
}
}
}
override init() {
super.init()
loadTableData()
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Interface Objects
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
//Check if session is supported and Activate
if (WCSession.isSupported()) {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
//Swift
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
let value = message["Value"]
dispatch_async(dispatch_get_main_queue()) {
self.objectsArray.removeAll()
self.objectsArray.append(value! as! Array)
self.loadTableData()
}
//send a reply
replyHandler(["Value":"Yes"])
}
}
苹果手机
我已经获取了所有对象并存储在数组中。
var objectsArray = [Objects]()
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
//send a reply
replyHandler(["Value": [objectsArray]])
}
我需要能够修改对象的属性并将更改保存在 iPhone 上,但 atm 我什至无法发送数据并显示在表中:( 我已经能够在设备之间的字典中发送简单的字符串值,但不能数组或实际数据。