Save textfield's string in an array - Swift -


i creating application that, written string in textfield, saves in array. array showed in tableviewcontroller embedded in mainviewcontroller. situation following. http://i.stack.imgur.com/z5utc.png. want every time tap green "save" button, string wrote in textfield saved , showed in tableview , textfield returns empty, can re-write string, added in array. tableview, in case, shows 2 strings, wrote in same textfield. tried write code 2 viewcontrollers (the mainviewvc , tablevc).

mainvc

import uikit  class mainvc: uiviewcontroller { @iboutlet var txtfield: uitextfield! var embtablevc: tablevc!  override func viewdidload() {  super.viewdidload()  // additional setup after loading view. }  override func didreceivememorywarning() {  super.didreceivememorywarning()  // dispose of resources can recreated. }  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {  if segue.identifier == "embededtablevc" {     embtablevc = segue.destinationviewcontroller as! tablevc  } }  @ibaction func save() {  if let text = txtfield.text {      if txtfield.text == "" {         myarray.append(text)         let row = myarray.count-1         let indexpath = nsindexpath(forrow: row, insection: 0)         embtablevc.mytableview.insertrowsatindexpaths([indexpath], withrowanimation: .fade)     } }  txtfield.text = ""  txtfield.resignfirstresponder() }      } 

tablevc

  import uikit    var myarray = [string]()    class tablevc: uitableviewcontroller {   @iboutlet var mytableview: uitableview! { didset {     mytableview.datasource = self     mytableview.delegate = self } }  override func viewdidload() {  super.viewdidload()  mytableview.datasource = self  mytableview.delegate = self  mytableview.registerclass(uitableviewcell.self, forcellreuseidentifier:   "customcell")  // additional setup after loading view, typically nib.  }  override func viewdidappear(animated: bool) {  super.viewdidappear(animated)  mytableview.reloaddata() }  override func didreceivememorywarning() {  super.didreceivememorywarning()  // dispose of resources can recreated.  }   override func numberofsectionsintableview(tableview: uitableview) -> int {  return 1   }   override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return myarray.count }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = mytableview.dequeuereusablecellwithidentifier("customcell", forindexpath: indexpath) uitableviewcell  cell.textlabel?.text = myarray[indexpath.item]  return cell } 

the code wrote doesn't work. can compile , run application on device, when tap green "save" button, app crashes. error message "fatal error: unexpectedly found nil while unwrapping optional value" on line: embtablevc.mytableview.insertrowsatindexpaths([indexpath], withrowanimation: .fade) in "save" ibaction. can me? lot:)

you trying add rows table view when not initialised. can is, save strings in array , pass array tablevc prepareforsegue method in mainvc. use array populate table view tablevc's cellforrowatindexpath: method.

in mainvc's save method, need append array, need code.

@ibaction func save() {  if let text = txtfield.text {      if txtfield.text == "" {         myarray.append(text)     } } 

i assume myarray variable property in tablevc. in prepareforsegue method this.

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {  if segue.identifier == "embededtablevc" {     embtablevc = segue.destinationviewcontroller as! tablevc     embtablevc.myarray = myarray  } } 

in tablevc's cellforrowatindexpath method, modify line set label text this.

cell.textlabel?.text = myarray[indexpath.row] 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -