【问题标题】:Issue when passing Data through segue in swiftswift中通过segue传递数据时的问题
【发布时间】:2015-05-31 10:53:17
【问题描述】:

我知道这个问题已经得到解答,但这并不能解决我的问题。问题是当我通过 segue 传递数据时,它在目标 ViewController 中返回 nil。 假设我在目标 ViewController 中定义了一个 News() 对象和三个字符串对象,我在 Segue 方法中设置了这些对象,但在目标 ViewController 中得到了 nil。

class NewsDetailViewController: UIViewController {
    var objNews = News();
    var newsTime: String?
    var newsDetail: String?
    var imageURL: String?

// rest of the class
.
.
}

这是我的segue方法

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Pass the selected object to the new view controller.
    if segue.identifier == "SegueNewsDetail" {
        var ndVC = NewsDetailViewController();
        var indexPath: NSIndexPath = self.tblViewStudent.indexPathForSelectedRow()!
        var tempObj = self.news.objectAtIndex(indexPath.row) as News
        ndVC.objNews = (self.news.objectAtIndex(indexPath.row) as News)
        ndVC.newsTime = tempObj.created_at;
        ndVC.newsDetail = tempObj.newsDescription;
        ndVC.imageURL = tempObj.imageURL;

        println("\(ndVC.newsTime) \(ndVC.newsDetail) \(ndVC.imageURL)")

    }

当我尝试在 segue 方法中记录值以检查值是否设置时(这些值设置为所需的值,我的意思是不要在 segue 方法中得到 nil):

println("\(ndVC.newsTime) \(ndVC.newsDetail) \(ndVC.imageURL)")

我尝试了很多选项,但都失败了。 任何人请帮助..

提前致谢

【问题讨论】:

    标签: ios swift null segue


    【解决方案1】:

    改变

    var ndVC = NewsDetailViewController()
    

    到

    let ndVC = segue.destinationViewController as! NewsDetailViewController
    

    顺便说一句,你在swift中不需要分号

    【讨论】:

    • 我有,但现在我收到错误“预期类型后为”。 @miracle-doh
    • 您使用的是 xcode 6.2 吗?如果是,请尝试使用 as 而不是 as!,因为 Xcode 6.3 需要作为感叹号来解开可选项
    • 太棒了!你能检查接受答案按钮吗,不胜感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2017-03-02
    相关资源
    最近更新 更多