没有为 WKInterfaceTable 调用 didSelectRowAtIndex

didSelectRowAtIndex not being called for WKInterfaceTable

我在情节提要中创建了一个从 WKInterfaceTableCell 到另一个 WKInterfaceController(称为 DetailInterfaceController)的推送转场。

当我点击该行时,didSelectRowAtIndex 没有被调用。

有谁知道我哪里出错了,我该如何传递字符串?

TableInterfaceController

didSelectRowAtIndexPath print 语句未被调用

@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.
}

为什么不叫:

table:didSelectRowAtIndex: 没有被调用是正常的,因为你使用了故事板转场。来自 WKInterfaceController 文档:

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

如何传递选中行数据:

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

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

请注意,table:didSelectRowAtIndex: 不会被另一个明显的原因所影响:WKInterfaceButton 被放置在行上并且用户点击它。在这种情况下,tap 事件只会为按钮触发。委托方法将不起作用。解决方案是从 WKInterfaceTable

的行中删除 WKInterfaceButton

我遇到了同样的问题,结果是我在故事板中取消选中了我的行项目的“可选”框