ios - swift segue to previous VC with data -
i looking perform 2 different segues.
i have vc entered "present modally". vc have segue(push) leading tableview vc.
problem 1
i want able go table view previous vc once touch row. problem here don't know type of segue use in order go 1 step sending data @ same time.
i have close button set "unwind vc" closing vc.
problem 2
from tableview have segue(push) leading tableview vc. - pick category , sub-category. problem here don't know how go 1 step or how make segue vc1 sends info both table view controllers.
thanks
problem 1:
on top of yourtableviewcontroller class (not inside class on top meaning outside) implement following protocol:
protocol mytableviewcontrollerdelegate { func tableviewcontroller(controller: yourtableviewcontroller, didfinishpicking item item: someitemyouwanttopassback) }
then following: in table view controller class(yourtableviewcontroller) implement these:
weak var delegate: mytableviewcontrollerdelegate? \the style implement delegates.
override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { let itemyouwanttopassbacktovc: someitemyouwanttopassback = dataimplementedintableview[indexpath.row] delegate?.tableviewcontroller(self, didfinishpickingitem item: itemyouwanttopassbacktovc) dismissviewcontrolleranimated(true, completion: nil) tableview.deselectrowatindexpath(indexpath, animated: true) }
then in previous vc implement function created in protocol:
func tableviewcontroller(controller: yourtableviewcontroller, didfinishpicking item item: someitemyouwanttopassback) { // here take "item" parameter , use purpose. item wanted pass here }
in addition read apple documentation learn more protocols , delegates.
problem 2:
ok... in case suggest following: implement following function in vc1:
@ibaction func unwindtovc1() { \\you can leave place empty }
then, pay attention these words: in storyboard find view controller tableview number 2 (the 1 showing subcategories) , on top of see 3 buttons. ctrl + drag yellow red exit door , choose "unwindtovc1" popup.
in document outline (the list view displaying have in storyboard) locate newly created segue , give name it, let "seguevc1".
then in tableviewcontrollernumbertwo implement this:
override func tableview(tableview: uitableview, didselectitematindexpath indexpath: nsindexpath) { let item1 = yourdatamodel[indexpath.row] performseguewithidentifier("seguevc1", sender: item1) } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let segue.identifier == "seguevc1" { let vc = segue.destinationviewcontroller vc1 \\ vc1 first vc want segue vc.modeltoreceive = sender modeltoreceiveclass } }
Comments
Post a Comment