【问题标题】:Pass data from view2 to view1 and update label将数据从 view2 传递到 view1 并更新标签
【发布时间】:2015-08-25 20:49:12
【问题描述】:

我有 view1 和 view2。在 view2 中,我设置了一个委托并输入了一些数据。当我按下保存按钮时,我 view2 我被发送到 view1。在 view1 中,我得到了在 view2 中输入的数据。但是...当我尝试使用 view2 中的数据更新 view1 中的标签或按钮文本时,什么也没有发生。 View1 未更新。

这是 view1 中应该更新标签的委托函数:

func getCurrentContact(ViewController: ViewControllerSettings, data: (ContactDetails)) {
    NSLog("Yes, the delegate is called")

    self.lblContact1.text = data.name1 as? String
}

有人知道为什么 lblContact1.text 没有更新吗?它只是保持与启动时相同。 data.name1 的 NSLog 包含我在 view2 中输入的正确数据。好像 view1 需要刷新什么的……

顺便说一句;有谁知道 GitHub 中的一个正在运行的 swift 项目或具有导航控制器和用于将数据从 view2 发送到 view1 的工作委托的东西?会很出色...

【问题讨论】:

  • 为什么不用 prepareForSegue 传递数据?
  • 嗯,我喜欢使用委托的想法。
  • 我也相信您应该在需要时使用 prepareForSegue,以便将数据从 view1 发送到 view2,并使用委托将数据从 view2 发送回 view1

标签: ios swift delegates


【解决方案1】:

尝试像这样重写您的代码以获得更多成功调试:

func getCurrentContact(ViewController: ViewControllerSettings, data: (ContactDetails)) {
    println("Yes, the delegate is called")

    if let name = data.name1 as ? String {
        println("Setting lblContact1 with \(name)")
        self.lblContact1.text = name
    } else {
        println("Either data was empty or data.name1 couldn't be extracted")
    }
}

如果您确实按预期收到了data.name1,那么我会检查您提供的代码是否实际上是running on the main thread

请注意,我还将 NSLog 替换为 println 以替换为 number of reasons

【讨论】:

  • 是的,println("Setting lblContact1 with (name)") 返回正确的数据。但是 self.lblContact1.text 没有更新。这让我发疯了......
猜你喜欢
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多