【问题标题】:prepareForSegue equivalent in WatchKit based on rowindex基于rowindex的WatchKit中的prepareForSegue等价物
【发布时间】:2016-02-17 23:58:01
【问题描述】:

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

我知道这涉及到使用contextForSegueWithIdentifier

有人可以解释如何将选定的行详细信息提供给目标接口控制器吗?

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

好的,这就是我 [作为我的控制器之一] 所在的位置,并假设如果我选择完整的日程安排,它将显示星期四 > 星期五 > 星期六 > 星期日的列表 .... 但如果我选择自定义它会显示名称 > 名称 > 名称 > 名称

【问题讨论】:

    标签: ios watchkit


    【解决方案1】:

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

    1. 在源接口控制器中,覆盖contextForSegueWithIdentifier,并检查情节提要segueIdentifier 以确定正在发生的转场。

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

    3. 最后,您返回的数据将成为目标接口控制器将访问的上下文。

      在您的示例中,您返回一个字符串,表示所选的日程安排类型:

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

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

      @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()
          }
      }
      

    【讨论】:

    • 好的,所以我有点理解这一点,所以它基本上是用一种可能的选项重新填充新控制器,对吧?所以我在我的代码中添加了我在第三个屏幕上使用的代码,你有时间将你的原则应用到我的代码和编辑中,以便我可以完全掌握你的想法吗?
    • 有没有关于这方面的具体教程或文章可以用来阅读并变得更好?
    • @conedmiro 我再次更新了示例。希望这将帮助您了解下一个控制器如何确定在其表中显示的内容。祝你好运!
    • 不得不将我的 [Push] 更改为 [Modal],然后一切正常!我什至看到一开始让我感到困惑的地方[在我得到你的帮助之前,我预先填充了太多额外的信息!非常感谢!!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多