ios - How to pass data from child to parent view controller? in swift -


i created activity when 1 of text fields clicked pop child(alert dialog) list of product when click 1 item on list can't display on text filed once alert dismissed.

this parent view

import foundation import uikit  class viewaward: uiviewcontroller{  @iboutlet var tfmcn: uitextfield! @iboutlet var tfamount: uitextfield! @iboutlet var tfproduct: uitextfield! @iboutlet var tftotal: uitextfield!  override func viewdidload() {     super.viewdidload()      let rightaddbarbuttonitem:uibarbuttonitem = uibarbuttonitem(title: "send", style: uibarbuttonitemstyle.plain, target: self, action: #selector(viewaward.searchtapped))      self.navigationitem.setrightbarbuttonitems([rightaddbarbuttonitem], animated: true)      let state = string(viewpopupproduct.product.productdescription)     print("my view state:"+state)      self.tfproduct.text = state     tfproduct.addtarget(self, action:  #selector(viewaward.producttapped), forcontrolevents: uicontrolevents.touchdown)  }  func searchtapped(sender:uibutton) {      let alertcontroller = uialertcontroller(         title: "award",         message:"award posted!",         preferredstyle: uialertcontrollerstyle.alert)     alertcontroller.addaction(uialertaction(title: "ok", style: uialertactionstyle.default,handler: nil))      self.presentviewcontroller(alertcontroller, animated: true, completion: nil) }  func producttapped(textfield: uitextfield){      //tfproduct.endediting(true)     tfproduct.resignfirstresponder()      let popovervc = uistoryboard(name:"main",bundle:nil).instantiateviewcontrollerwithidentifier("sbpopupid") as! viewpopupproduct      self.addchildviewcontroller(popovervc)      popovervc.view.frame = self.view.frame      self.view.addsubview(popovervc.view)      popovervc.didmovetoparentviewcontroller(self)  } } 

and when user clicked on of items

import uikit  class viewpopupproduct: uiviewcontroller {  @iboutlet var tableview: uitableview!  var productdescription = ["product 1","product 2","product 3"] var productid = ["prdct1","prdct2","prdct3"]   // global variables  struct product {     static var productid = string()     static var productdescription = string() }  override func viewdidload() {     super.viewdidload()     self.showanimate()     self.view.backgroundcolor = uicolor.blackcolor().colorwithalphacomponent(0.4)      // additional setup after loading view. }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  @ibaction func cancelpopup(sender: anyobject) {     self.removeanimate() }   func showanimate() {     self.view.transform = cgaffinetransformmakescale(1.3, 1.3)     self.view.alpha = 0.0;     uiview.animatewithduration(0.25, animations: {         self.view.alpha = 1.0         self.view.transform = cgaffinetransformmakescale(1.0, 1.0)     }); }  func removeanimate() {     uiview.animatewithduration(0.25, animations: {         self.view.transform = cgaffinetransformmakescale(1.3, 1.3)         self.view.alpha = 0.0;         }, completion:{(finished : bool)  in             if (finished)             {                 self.view.removefromsuperview()             }     }); }  //mark - table view  func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     return self.productid.count }  func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = self.tableview.dequeuereusablecellwithidentifier("cell",forindexpath: indexpath) as! productviewcell      cell.productlabel.text = productdescription[indexpath.row]      return cell }  func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {      tableview.deselectrowatindexpath(indexpath, animated: true)      product.productid = string(productid[indexpath.row])     product.productdescription = string(productdescription[indexpath.row])      self.removeanimate()  }  } 

you can use protocols/delegate

here very straightforward explanation, no bs: https://www.youtube.com/watch?v=gusympaxlaw

or in situation can use nsnotificationcenter

you can this:

the "sender" view controller do

let nc = nsnotificationcenter.defaultcenter() nc.postnotificationname("printvalue", object: nil, userinfo: ["value" : "pass me string"]) 

the receiver view controller can listen notification.

let nc = nsnotificationcenter.defaultcenter() nc.addobserver(self, selector: #selector(printvalue), name: "printvalue", object: nil)  func printvalue(notification:nsnotification) {     let userinfo:dictionary<string,string> = notification.userinfo as! dictionary<string,string>     let item = userinfo["value"]! string      print(item,self) } 

Comments