ios - Pass data from one view to another in swift programatically -


i've searched site , threads i've found around subject remain unanswered hoped have more luck.

i relatively sure trying easy can't seem find answer.

i have view controller uitext field when text field clicked presents search controller modally.

here code text field setalerttablecontroller.swift:

override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell      if indexpath.section == 0 {          cell.textlabel!.text =  "set station alert"         cell.backgroundview = uiimageview(image: uiimage(named: "red-full"))         cell.textlabel?.backgroundcolor = uicolor.whitecolor().colorwithalphacomponent(0.0)         cell.imageview!.image = uiimage(named: "metro-no-pad")         cell.textlabel?.textcolor = uicolor.whitecolor()          var searchfield = uitextfield(frame: cgrectmake(60.0, 25.0, 250.0, 30.0))         searchfield.backgroundcolor = uicolor.whitecolor()         searchfield.borderstyle = uitextborderstyle.line         searchfield.borderstyle = .roundedrect         searchfield.placeholder = "search stations"         searchfield.textcolor = uicolor.lightgraycolor()         searchfield.delegate = self         self.view.addsubview(searchfield)        } else if indexpath.section == 1 {          cell.backgroundview = uiimageview(image: uiimage(named: "red-full"))         cell.userinteractionenabled = false          var slider=uislider(frame:cgrectmake(10, 120, 300, 10));         slider.minimumvalue = 0;         slider.maximumvalue = 5;         slider.continuous = false;         slider.value = 0;         slider.addtarget(self, action: "slidervaluedidchange:", forcontrolevents: .valuechanged);         self.view.addsubview(slider);       } else if indexpath.section == 2 {          cell.textlabel!.text =  "set alert"         cell.backgroundview = uiimageview(image: uiimage(named: "red-full"))         cell.textlabel?.backgroundcolor =      uicolor.whitecolor().colorwithalphacomponent(0.0)         cell.imageview!.image = uiimage(named: "confirm")         cell.textlabel?.textcolor = uicolor.whitecolor()         cell.accessorytype = uitableviewcellaccessorytype.disclosureindicator      }      return cell  } 

when search filters results in table view cells. when correct results cell pressed need dismiss modal view , show data selected cell in textfield.

here code search controller (viewcontroller.swift) stands:

import uikit import foundation  class viewcontroller: uiviewcontroller {     let kcellidentifier = "cell"      var searchcontroller: uisearchdisplaycontroller!     var tableview: uitableview!     var tabledata: [string]? {         didset {             self.tableview.reloaddata()         }     }     var tabledatafiltered = [string]()      override func viewdidload() {         super.viewdidload()          tableview = uitableview(frame: cgrectzero, style: .plain)         tableview.registerclass(uitableviewcell.self, forcellreuseidentifier: kcellidentifier)         tableview.delegate = self         tableview.datasource = self         self.view.addsubview(tableview)          let searchbar = uisearchbar()         searchbar.sizetofit()         searchbar.becomefirstresponder()         //tableview.tableheaderview = searchbar         self.navigationitem.titleview = searchbar;          searchcontroller = uisearchdisplaycontroller(searchbar: searchbar, contentscontroller: self)         searchcontroller.searchresultsdatasource = self         searchcontroller.searchresultsdelegate = self         searchcontroller.searchresultstableview.registerclass(uitableviewcell.self, forcellreuseidentifier: kcellidentifier)           tabledata = ["bournemouth", "branksome", "parkstone"]          let cancelbutton = uibarbuttonitem(title: "cancel", style: uibarbuttonitemstyle.plain, target: self, action: "dismissview")         self.navigationitem.rightbarbuttonitem = cancelbutton      }  func dismissview() {      dismissviewcontrolleranimated(true, completion: nil)  }  func searchbar(searchbar: uisearchbar, textdidchange searchtext: string) {         println(searchbar.text)  }      override func viewdidlayoutsubviews() {         super.viewdidlayoutsubviews()         tableview.frame = self.view.bounds     } }  extension viewcontroller: uitableviewdatasource {     func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         if tableview != self.tableview {             return self.tabledatafiltered.count         }          if let count = tabledata?.count {             return count         }         return 0     }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell: uitableviewcell = tableview.dequeuereusablecellwithidentifier(kcellidentifier, forindexpath: indexpath) as! uitableviewcell          var data: string         if tableview != self.tableview {             data = tabledatafiltered[indexpath.row]         } else {             data = tabledata![indexpath.row]         }          cell.textlabel?.text = data         return cell     } }   extension viewcontroller: uitableviewdelegate {     func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {         if let data = tabledata?[indexpath.row] {             dismissviewcontrolleranimated(true, completion: nil)             println(indexpath.row)            }     } } 

everything works should @ stage, dismissing modal view when clicked, can't work out way pass result previous controller's text field. @ appreciated.

what best or more practical way without storyboards?

people claiming question duplicate of previous posts , on verge of post being removed, of other posts either in objective-c not swift or rely heavily on storyboard , segue use.

unfortunately can't use segues without storyboard.

there's multiple ways this, 1 recommend (as have experience delegates having used uitableviewcontroller) create swift protocol acts delegate on uisearchcontroller this:

import uikit  protocol viewcontrollerdelegate {      func searchviewcontrollerdidselectresult(searchvc:viewcontroller,result: string) }  class viewcontroller: uiviewcontroller {  var delegate: viewcontrollerdelegate?  func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {      if let data = tabledata?[indexpath.row] {          if let delegate = self.delegate {              delegate.searchviewcontrollerdidselectresult(self, result: data)         } //            dismissviewcontrolleranimated(true, completion: nil) // dismiss search view controller within delegate method of view controller text field         println(indexpath.row)      } } } 

the view controller text field this. setalerttableviewcontroller needs conform viewcontrollerdelegate so:

class setalerttableviewcontroller: uitableviewcontroller, viewcontrollerdelegate 

and implement method declared in protocol set text in text field:

func searchviewcontrollerdidselectresult(searchvc:viewcontroller, result: string) {      // keep track of result     self.result = result     self.tableview.reloaddata()     self.dismissviewcontrolleranimated(true, completion: nil) } 

you'll need make sure when show viewcontroller search results set delegate on know it's delegate is! if need more help, let me know!


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -