【问题标题】:Unable to Pass Data Between Interface Controllers无法在接口控制器之间传递数据
【发布时间】:2015-04-16 16:15:59
【问题描述】:

我在将字符串传递给 WatchKit 应用程序中的新接口控制器时遇到问题。新界面控制器的转场从表格行连接到情节提要中。这是我选择表格行的代码:

  var selectedElement: String!
  override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
    switch rowIndex{
    case 0:
        selectedElement = menuElements.objectAtIndex(0) as! String
    case 1:
        selectedElement = menuElements.objectAtIndex(1) as! String
    case 2:
        selectedElement = menuElements.objectAtIndex(2) as! String
    default:
        break
    }
}

那我就是用这个方法把 selectedElement 变量作为上下文来传递的:

  override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
    return selectedElement
 }

在我的 SecondInterfaceController 类中,我尝试使用此代码在标签中显示上下文字符串:

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    label.setText(context as? String)
 }

这会产生一个空白标签,我也尝试将上下文打印到控制台,结果返回 nil。

提前感谢您的帮助。

【问题讨论】:

  • 你到底想展示什么?
  • 我正在尝试在第二个界面控制器的标签中显示连接到表视图行的字符串。

标签: swift watchkit


【解决方案1】:

要在接口控制器之间传递数据,您需要添加此方法

pushControllerWithName:context:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
  • 2015-12-16
  • 2017-01-12
相关资源
最近更新 更多