gpt4 book ai didi

ios - 在 swift 中通过 segue 传递数据时出现问题

转载 作者:可可西里 更新时间:2023-11-01 01:05:51 24 4
gpt4 key购买 nike

我知道这个问题已经得到解答,但这并不能解决我的问题。问题是当我通过 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)")

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

提前致谢

最佳答案

改变

var ndVC = NewsDetailViewController()

let ndVC = segue.destinationViewController as! NewsDetailViewController

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

关于ios - 在 swift 中通过 segue 传递数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com