ios – 没有为WKInterfaceTable调用didSelectRowAtIndex

前端之家收集整理的这篇文章主要介绍了ios – 没有为WKInterfaceTable调用didSelectRowAtIndex前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在故事板中创建了一个push segue,从WKInterfaceTableCell到另一个WKInterfaceController(称为DetailInterfaceController).

当我点击该行时,未调用didSelectRowAtIndex.

有人知道我哪里错了,我怎么能传递字符串?

TableInterfaceController

调用didSelectRowAtIndexPath打印语句

@IBOutlet var table: WKInterfaceTable!
var objectsArray = ["1","2","3"]
var object: String!

override func table(table: WKInterfaceTable,didSelectRowAtIndex rowIndex: Int) {
    self.object = self.objectsArray[rowIndex]

    print(" object title: \(self.object)")
}


override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
    // You may want to set the context's identifier in Interface Builder and check it here to make sure you're returning data at the proper times

    // Return data to be accessed in ResultsController
    return self.object
}

DetailsInterfaceController

标签未设置

@IBOutlet var label: WKInterfaceLabel!

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    // Make sure data was passed properly and update the label accordingly
    if let val: String = context as? String {
        self.label.setText(val)
    } else {
        self.label.setText("")
    }
    // Configure interface objects here.
}

解决方法

为什么不叫:

表格是正常的:didSelectRowAtIndex:因为你使用了故事板segue而没有被调用.从WKInterfaceController文档:

If you connected an action method to the table in your storyboard file,WatchKit does not call this method.

如何传递选定的行数据:

您应该使用contextForSegueWithIdentifier:inTable:rowIndex:返回所选行的上下文:

override func contextForSegueWithIdentifier(segueIdentifier: String,inTable table: WKInterfaceTable,rowIndex: Int) -> AnyObject? {
    if segueIdentifier == "someSegueIdentifier" {
        return objectsArray[rowIndex]
    }
    return nil
}

猜你在找的iOS相关文章