ios - Unable to display data in the second view controller using segue [Swift] -
i need pass data 1 view controller view controller. used segue (detail) , define model class named "photo".
tableviewcontroller looks following:
var photos = [photo]() //strongly typed swift array override func viewdidload() { super.viewdidload() var newphoto = photo(name:"cat ", filename:"cat", notes:"cat_file") photos.append(newphoto) var newphoto2 = photo(name:"r2 ", filename:"r2", notes:"r2") photos.append(newphoto2) }
and other view controller (detailviewcontroller) looks following:
import uikit class photodiplayviewcontroller: uiviewcontroller { var currentphoto: photo? @iboutlet weak var currentimage: uiimageview! @iboutlet weak var currentlabel: uilabel! override func viewdidload() { super.viewdidload() var image = uiimage(named: currentphoto!.filename) self.currentimage.image = image self.currentlabel.text = currentphoto?.name println(currentphoto!.name + currentphoto!.filename + currentphoto!.notes) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
when running program, table view loading fine , if click on cell going detail view controller. noting there in detail view controller. , used println() check , output coming in debugger following:
cat cat cat_file
to pass data, used following segue code block:
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using [segue destinationviewcontroller]. // pass selected object new view controller. var secondscene = segue.destinationviewcontroller as! photodiplayviewcontroller if let indexpath = self.tableview.indexpathforselectedrow(){ let selectedphoto = photos[indexpath.row] secondscene.currentphoto = selectedphoto } }
but still no luck! tried figure out missing? can tell me lagging?
update: complete detail view controller class code
update: full detail of table view code
import uikit class phototableviewcontroller: uitableviewcontroller { var photos = [photo]() //strongly typed swift array override func viewdidload() { super.viewdidload() var newphoto = photo(name:"cat ", filename:"cat", notes:"cat_file") photos.append(newphoto) var newphoto2 = photo(name:"r2 ", filename:"face.jpg", notes:"r2") photos.append(newphoto2) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { // #warning potentially incomplete method implementation. // return number of sections. return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete method implementation. // return number of rows in section. return photos.count } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("photocell", forindexpath: indexpath) as! uitableviewcell var currentphoto = photos[indexpath.row] cell.textlabel?.text = currentphoto.name return cell } /* // override support conditional editing of table view. override func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool { // return no if not want specified item editable. return true } */ /* // override support editing table view. override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if editingstyle == .delete { // delete row data source tableview.deleterowsatindexpaths([indexpath], withrowanimation: .fade) } else if editingstyle == .insert { // create new instance of appropriate class, insert array, , add new row table view } } */ /* // override support rearranging table view. override func tableview(tableview: uitableview, moverowatindexpath fromindexpath: nsindexpath, toindexpath: nsindexpath) { } */ /* // override support conditional rearranging of table view. override func tableview(tableview: uitableview, canmoverowatindexpath indexpath: nsindexpath) -> bool { // return no if not want item re-orderable. return true } */ // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using [segue destinationviewcontroller]. // pass selected object new view controller. var secondscene = segue.destinationviewcontroller as! photodiplayviewcontroller if let indexpath = self.tableview.indexpathforselectedrow(){ let selectedphoto = photos[indexpath.row] secondscene.currentphoto = selectedphoto } } }
don't use segue. use this, easier.
follow these steps...
1: create separate file called manager.swift , place code in it...
//manager.swift import foundation struct manager { static var datatopass = string() }
2: clean project pressing shift+command+k.
3: in first view controller set datatopass data want pass...
manager.datatopass = self.datatopass
4: in second view controller retrieve data , set content datatopass...
self.datatoreceive = manager.datatopass
5: finished!!
Comments
Post a Comment