ios - Prepare for segue is autocalled -
i have view crontroller called "subcateory2", viewcontroller has uicollectionview wit custom cell. need 2 segues app. 1 of called "to_videostable" viewcontroller other view controller , other calles "reload_collection" cell same viewcontroller(because subcategory can have n-level of subcategories). problem prepareforsegue(i check in function identifier , defined in " func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath)",and execute different actions). when select cell should happen:
first: go "func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath)", check condition , define identifier segue.
second: go prepareforsegue, check condition , execute actions.
but happen:
- first: in ios simulator select cell.
second: code go prepareforsegue , go segue called "reload_collection"(before going func collectionview(...)), , create white views. in moment 2 threads created, 1 of them go white windows , other next stop.
third: "second theard" go func collectionview(...) , check condition, define identifier, call performseguewithidentifier , go prepareforsegue function. in prepareforsegue check identifier , execute differentes actions.
this code:
import uikit class subcategory2: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdatasource { let viewutils = viewcontrollerutils() var result_category: array<json> = [] @iboutlet weak var collectionview: uicollectionview! var tit: string! var url: string! var end_url:string = "?page_size=100" var id_category: int! var index: nsindexpath! var url_children : string = "" let imagepath = "http://d1rkb03u2ginv9.cloudfront.net/wp-content/uploads/" override func viewdidload() { self.viewutils.showactivityindicator(self.view) super.viewdidload() self.viewutils.showactivityindicator(self.view) if self.result_category.isempty{ var category = category_function() self.url = self.url + self.end_url self.result_category = category.load_subcategory(self.url)} self.viewutils.hideactivityindicator(self.view) // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { var cell : uicollectionviewcell = collectionview.cellforitematindexpath(indexpath)! println("entroooooooo") if (self.result_category[indexpath.row]["children"].string != nil){ self.url_children = self.result_category[indexpath.row]["children"].string! //while(self.url_children.isempty){} println("voy reloadcollection") performseguewithidentifier("reload_collection", sender: cell) //performseguewithidentifier("reload_collection3", sender: self) }else{ println("voy to_videostables") performseguewithidentifier("to_videostable", sender: cell) } } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { //println(result_category.count) return result_category.count } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) ->uicollectionviewcell { let cell: collectionviewcellcontroller2 = collectionview.dequeuereusablecellwithreuseidentifier("cell2", forindexpath: indexpath) as! collectionviewcellcontroller2 println(self.result_category[indexpath.row]["slug"].stringvalue) cell.label.text = self.result_category[indexpath.row]["slug"].stringvalue if (self.result_category[indexpath.row]["images"]["file"].string != nil){ //println("+++++++++++++++++") var image = self.result_category[indexpath.row]["images"]["file"].stringvalue cell.image.sd_setimagewithurl(nsurl(string:self.imagepath + (image as! string))) }else{ var image = "http://www.camping-oaza.com/images/joomlart/demo/default.jpg" cell.image.sd_setimagewithurl(nsurl(string: image)) //cell.image.image = uiimage(named: image) } cell.numbervideoslabel.text = self.result_category[indexpath.row]["videos_count"].stringvalue cell.numbersubcategorylabel.text = self.result_category[indexpath.row]["children_count"].stringvalue return cell } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "to_videostable"{ println("-------------") println("voy to_videostables") let cell = sender as! uicollectionviewcell let index = self.collectionview!.indexpathforcell(cell) // return -> nsindexpath? //if (self.result_category[index!.row]["children"].string != nil){ // self.loaddata(self.result_category[index!.row]["children"].string!) //}else{ let vc : videosviewcontroller = segue.destinationviewcontroller as! videosviewcontroller println(self.result_category[index!.row]["id"].intvalue) vc.id_category = self.result_category[index!.row]["id"].intvalue } if segue.identifier == "to_livevideos"{ println("-------------") println("to_livevideos") println("-------------------") let vc : swrevealviewcontroller = segue.destinationviewcontroller as! swrevealviewcontroller } if segue.identifier == "reload_collection"{ println("-------------") println("reload_collection") println("-------------------") var category = category_function() let vc : subcategory2 = segue.destinationviewcontroller as! subcategory2 vc.url = self.url_children println(category.load_subcategory(self.url_children + self.end_url)) } } }
with problem, created white windows , after created windows real information.
the order of println : - "reload_collection" - "entroooooooo" - "voy reloadcollection" or "voy to_videostables"
updated answer
you have situation want decide segue take when cell selected. have wired 1 of segues directly cell, means storyboard create segue you. calling performseguewithidentifier
creates segue. need implement shouldperformseguewithidentifier
cancel "reload_collection" segue when want segue "to_videostables".
in original answer below, suggested wire both segues viewcontroller, won't work because 1 of segues same viewcontroller.
so, way to:
modify
didselectitematindexpath
remove code handles "reload_collection" segue. storyboard making segue:func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { var cell : uicollectionviewcell = collectionview.cellforitematindexpath(indexpath)! println("entroooooooo") if result_category[indexpath.row]["children"].string == nil { println("voy to_videostables") performseguewithidentifier("to_videostable", sender: cell) } }
wire segue "reload_collection" cell viewcontroller. allow storyboard perform segue you.
implement
shouldperformseguewithidentifier
tell storyboard when should make segue:override func shouldperformseguewithidentifier(identifier: string, sender: anyobject?) -> bool { if segue.identifier == "reload_collection" { let indexpath = collectionview.indexpathforcell(sender as! uicollectionviewcell) return result_category[indexpath.row]["children"].string != nil } return true }
in
prepareforsegue
need seturl_children
since no longer being donedidselectitematindexpath
:if segue.identifier == "reload_collection"{ println("-------------") println("reload_collection") println("-------------------") var category = category_function() let vc : subcategory2 = segue.destinationviewcontroller as! subcategory2 let indexpath = collectionview.indexpathforcell(sender as! uicollectionviewcell) url_children = result_category[indexpath.row]["children"].string! vc.url = url_children println(category.load_subcategory(self.url_children + self.end_url)) }
original answer
your segue getting auto-called because have wired cell. if want trigger performseguewithidentifier
needs wired viewcontroller
other segue. remove segue cell , rewire viewcontroller , give same identifier had when wired cell , should work.
Comments
Post a Comment