我试图在 TableView 和 View Controller 之间传递数据。 由于我不在这里重复的原因,我以编程方式创建了 View Controller ,只是通过代码并且没有任何类型的引用和 Storyboard中的存在(我没有 Storyboard中的 ViewController)。一切正常,还有图形转换,但数据转换。 在图形转换之前,我在“didSelectRowAtIndexPath”中执行了数据转换。
在 View Controller 中
var purchase:Purchase?
在 TableViewController 中
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var arrayToUse:NSArray?
var tableInUse:UITableView?
let dc = DetailsViewController()
// Retrive the correct data here
let indexPath = tableInUse?.indexPathForSelectedRow
dc.purchase = arrayToUse?.objectAtIndex((indexPath?.row)!) as! Purchase
let temp = arrayToUse?.objectAtIndex((indexPath?.row)!) as! Purchase
print(temp.title)
self.navigationController!.pushViewController(DetailsViewController(), animated: true)
}
我查看了在这里和其他站点找到的不同解决方案,但我总是找到考虑“segue”或“nib”的解决方案。 我做错了什么?
我希望我说清楚了 非常欢迎各种帮助或提示。
谢谢
最佳答案
问题是您正在创建 DetailsViewController
实例并为其设置数据,但推送另一个 DetailsViewController
对象。您应该使用以前创建的 dc
对象而不是创建新实例。
代替:
self.navigationController!.pushViewController(DetailsViewController(), animated: true)
你应该使用:
self.navigationController!.pushViewController(dc, animated: true)
关于ios - 在没有任何 Storyboard引用的情况下在 Controller 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640509/