【问题标题】:Optional binding in Swift not working in prepareForSegueSwift 中的可选绑定在 prepareForSegue 中不起作用
【发布时间】:2015-08-13 05:04:37
【问题描述】:

我正在准备转至 TabViewController 标题 SmashTabController,它连接到标题为 SmashTableView 的 TableViewController - 如果您现在还没有猜到,我正在关注斯坦福 193P iOS 课程。

在我的调试会话中,我看到我的目标 VC 是 SmashTabController,但由于某种原因,我的可选绑定总是失败。

知道为什么会发生这种情况吗?

提前非常感谢您!

if let clicked = sender as? MentionsTableViewCell {
    if clicked.mentions != nil {
        let urlString = clicked.mentions!
        if let url = NSURL(string: urlString) {
            UIApplication.sharedApplication().openURL(url)
        }
    } else if let stvc = destination as? SmashTabViewController {
        if let svc = stvc.presentedViewController as? SmashTableViewController {
            svc.newSearch = clicked.mentions
        }
    }
}

【问题讨论】:

  • if let clicked 处设置一个断点,并逐行查看哪一行失败。一旦你将它缩小到一行,就会更容易找到问题所在。
  • 我已经完成了,这是给 stvc 的任务。它总是失败,我不明白

标签: ios swift binding optional cs193p


【解决方案1】:
if let clicked = sender as? MentionsTableViewCell {
        if clicked.mentions != nil {
            let urlString = clicked.mentions!
                if let url = NSURL(string: urlString) {
                        UIApplication.sharedApplication().openURL(url)
                }
            } else if let stvc = segue.destinationViewController as? SmashTabViewController {
            if let svc = stvc.presentedViewController as? SmashTableViewController {
                        svc.newSearch = clicked.mentions
                    }
        }
    }

我看不到您在prepareForSeque 中实际给出destinationViewController 的位置。我假设它是 TabBarController,然后您想将数据传递给该 tabBar 中的视图。如果是这种情况,这应该有效。

【讨论】:

    【解决方案2】:

    我不确定你在 else-if 部分到底在做什么。我的代码通常如下所示:

    if let controller = segue.destinationViewController as? TheDestinationViewController
    

    或者如果目标控制器包含在导航控制器中,则类似于:

            if let nav = segue.destinationViewController as? UINavigationController {
                if let targetController = nav.topViewController as? TheDestinationViewController {
                    // pass on something?
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2015-09-22
      • 2016-09-05
      相关资源
      最近更新 更多