ios - 在没有任何 Storyboard引用的情况下在 Controller 之间传递数据

标签 ios swift uitableview uiviewcontroller

我试图在 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/

相关文章:

ios - Switch在检查table view tag时,会陆续调用多个case

ios - UITableView 中的右侧细节不可见

ios - swift iOS - 当角色离开屏幕时无法重置游戏场景

IOS - UITableController 部分页脚的背景颜色

ios - 使用 xcode 将 IOS 静态库架构设置为 "arm64"

ios - swift 3 json序列化

ios - Chromecast 在 iOS 上的实现

ios - 核心数据保存关闭

ios - 使用部分类别填充表格 View

ios - 如何检查 UITableViewCell 是否包含 UIView