gpt4 book ai didi

ios - WatchKit 中基于 rowindex 的 prepareForSegue 等价物

转载 作者:行者123 更新时间:2023-11-29 12:04:42 25 4
gpt4 key购买 nike

我一直在尝试根据所选行在 WatchKit 中设置分层表。

我知道这涉及使用 contextForSegueWithIdentifier

有人可以解释如何将所选行的详细信息提供给目标接口(interface) Controller 吗?

@IBOutlet var mainTable: WKInterfaceTable!

let mains = ["Full Schedule", "Custom Sched."]

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

loadTableData()
}

private func loadTableData() {

mainTable.setNumberOfRows(mains.count, withRowType: "InterfaceTableRowController")

for (index, mainName) in mains.enumerate() {

let row = mainTable.rowControllerAtIndex(index) as! InterfaceTableRowController

row.interfaceLabel.setText(mainName)
}

}

override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject?
{

let mainName = mains[rowIndex]
return mainName
}


override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
NSLog("%@ will activate", self)
}

好的 所以这就是我 [作为我的一个 Controller ] 的位置,假设如果我选择完整的时间表,它将显示星期四 > 星期五 > 星期六 > 星期日的列表......但如果我选择自定义它会显示姓名 > 姓名 > 姓名 > 姓名

最佳答案

简而言之,它与 prepareForSegue 有点相似,但您返回一个上下文以传递给目标接口(interface) Controller ,而不是直接在目标接口(interface) Controller 上设置属性。

  1. 在源接口(interface) Controller 中,您覆盖 contextForSegueWithIdentifier,并检查 Storyboard segueIdentifier 以确定正在发生哪个 segue。

  2. 接下来,您使用 rowIndex 从其 mains 数组中检索该行的数据。

  3. 最后,您返回的数据将成为目标接口(interface) Controller 将访问的上下文。

    在您的示例中,您返回一个表示所选计划类型的字符串:

    override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? {
    if segueIdentifier == "showSchedule" {
    return mains[rowIndex]
    }
    return nil
    }
  4. 在目标接口(interface) Controller 中,您访问提供的上下文。

    在这种情况下,它是所选计划的类型。您将为该类型的计划配置数组,然后填充表格。

    @IBOutlet weak var scheduleTable: WKInterfaceTable!

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

    // Configure table here.
    guard let context = context as? String else {
    return
    }

    // Load the table based on the type of (full or custom) schedule
    if context == "Full schedule" {
    loadFullSchedule()
    } else if context == "Custom Sched." {
    loadCustomSchedule()
    }
    }

关于ios - WatchKit 中基于 rowindex 的 prepareForSegue 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35470188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com