this question has answer here:
after spending days searching solution, reading developer documentation , threads such passing data between view controllers attempting pass data through number of views @ loss.
my application pc bundle builder , utilises 3 tableviewcontrollers. first loads pcs, second monitors , third accessories. when cell tapped next view tableview loads. application loads data three, great! however, create summary page display selected pc, monitor , accessory information (image, name , description).
using pcs (the first view controller) example:
data initialised in pcs.swift
class pc { //si. mark: pc properties //si. variables because change 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 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. validation if statement has been if name.isempty || graphics.isempty || cpu.isempty || hdd < 0 || ram < 0 || sku < 0 || price.isempty { return nil } } }
test objects created in pctableviewcontroller
, placed in array. loadtestpcs
placed in viewdidload()
:
//si. initialises pcs empty array of objects monitors() store pc details. var pcs = [pc]() //si. creates data in 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 , 3 pcs array. // pcs += [pc1!, pc2!, pc3!] }
then have used following retrieve desired data tapped cell:
override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { //si. created constant pctableviewcell set earlier reuse identifier prototype cell in table view controller. //si. type of cell needs downcast 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) }
my problem how pass currentpc
next tableviewcontroller
(where currentmonitors
used store selected monitor. passed on (collecting selected accessory) , finally, data passed summary view labels used outlets display selected options.
the question ask how handle on next tableviewcontroller
within first tableviewcontroller
. answer depends on how you're instantiating second tvc -- using storyboards, or initializing in code , pushing manually?
storyboard case:
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 }
manual case:
override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { (...) let nexttableview = pctableviewcontroller() nexttableview.pctodisplay = currentpc self.navigationcontroller?.pushviewcontroller(nexttableview, animated: true) }
Comments
Post a Comment