花了几天时间搜索解决方案后,阅读开发人员文档和线程,例如 Passing Data between View Controllers 试图通过一些观点传递数据我不知所措。
我的应用程序是一个PC Bundle构建器,目前使用三个tableViewControllers。第一个加载PC,第二个监视器和第三个附件。当点击一个单元格时,下一个视图TableView会加载。应用程序当前将数据加载到这三个中,这太棒了!但是,我想创建一个摘要页面来显示所选的PC,监视器和附件信息(图像,名称和描述)。
以PC(第一个视图控制器)为例:
数据在PCs.swift
class Pc {
//SI. MARK: pc properties
//SI. variables because they will change for each cell.
var image: UIImage?
var name: String
var graphics: String
var cpu: String
var hdd: Int
var ssd: Int?
var ram: Int
var sku: Int
var price: String
//SI. set the initial value of each variable
init?(image: UIImage?, name: String, graphics: String, cpu: String, hdd: Int, ssd: Int?, ram: Int, sku: Int, price: String) {
self.image = image
self.name = name
self.graphics = graphics
self.cpu = cpu
self.hdd = 1
self.ssd = nil
self.ram = ram
self.sku = 12345
self.price = price
//SI. for validation an if statement has been
if name.isEmpty || graphics.isEmpty || cpu.isEmpty || hdd < 0 || ram < 0 || sku < 0 || price.isEmpty {
return nil
}
}
}
测试对象在PcTableViewController
中创建并放置在一个数组中。 loadTestPcs
位于viewDidLoad()
:
//SI. Initialises pcs with an empty array of objects from the Monitors() to store PC details.
var pcs = [Pc]()
//SI. Creates data in a function "loadTestPcs".
func loadTestPcs()
{
let pic1 = UIImage(named: "PC1")
let pc1 = Pc(image: pic1, name: "PC1", graphics: "Radeon R7", cpu: "A6", hdd: 1, ssd: 0, ram: 8, sku: 111110, price: "499.99")
let pic2 = UIImage(named: "PC2")
let pc2 = Pc(image: pic2, name: "PC2", graphics: "Radeon R7", cpu: "A6", hdd: 1, ssd: 0, ram: 8, sku: 111112, price: "549.99")
let pic3 = UIImage(named: "PC3")
let pc3 = Pc(image: pic3, name: "PC3", graphics: "Radeon R7", cpu: "A6", hdd: 1, ssd: 0, ram: 8, sku: 111113, price: "599.99")
//SI. adds details stored in pc1,2 and 3 to pcs array.
//
pcs += [pc1!, pc2!, pc3!]
}
然后我使用以下内容从窃听的单元格中检索所需的数据:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//SI. created a constant for pcTableViewCell which was set earlier as the reuse identifier for the prototype cell in the table view controller.
//SI. type of cell needs to be downcast to custom cell subclass (pcTableViewCell).
let cellIdentifier = "pcTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! pcTableViewCell
var currentPC = []
let pc = pcs[indexPath.row]
cell.picImageView.image = pc.image
cell.nameLabel.text = pc.name
cell.priceLabel.text = pc.price
currentPC = [cell.picImageView.image!, cell.nameLabel.text!, cell.priceLabel.text!]
print(currentPC)
}
我现在的问题是如何将currentPC
传递给下一个TableViewController
(其中currentMonitors
将用于存储所选的监视器。这将被传递(收集选定的附件)最后,数据将传递到摘要视图,其中用作出口的标签将显示所选选项。
答案 0 :(得分:0)
问自己的问题是如何在第一个tableViewController
内处理下一个tableViewController
。答案是,这取决于你如何实例化第二个tvc - 你是在使用故事板,还是在初始化代码并手动推送?
var pcToDisplay: Array<AnyObject>?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
(...)
self.pcForNextTableView = currentPC
performSegueWithIdentifier("nextTableView", sender: nil)
}
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "nextTableView", let nextTableView = segue.destinationViewController as? PCTableViewController {
nextTableView.pcToDisplay = self.pcForNextTableView
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
(...)
let nextTableView = PCTableViewController()
nextTableView.pcToDisplay = currentPC
self.navigationController?.pushViewController(nextTableView, animated: true)
}