在没有任何类型的故事板参考的情况下在控制器之间传递数据

时间:2016-06-05 09:45:41

标签: ios swift uitableview uiviewcontroller

我试图在tableview和viewcontroller之间传递数据。 由于我不在这里重复,我以编程方式创建了视图控制器,只是通过代码而没有任何参考和在故事板中的存在(我没有在Storyboard中的ViewController)。一切正常,也是图形转换,但数据转换。 我在" didSelectRowAtIndexPath"中执行了数据转换。在图形转换之前。

在ViewController中

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"。 我做错了什么?

我希望我明确表示 我们非常欢迎各种帮助或提示。

谢谢

1 个答案:

答案 0 :(得分:4)

问题是您正在创建DetailsViewController的实例并将数据设置为它,但推送另一个DetailsViewController对象。您应该使用以前创建的dc对象,而不是创建新实例。

而不是:

self.navigationController!.pushViewController(DetailsViewController(), animated: true)

您应该使用:

self.navigationController!.pushViewController(dc, animated: true)