swift - Error Saving Text Views in NSUserDefault -
i have 6 textviews , 1 button save text wrote in them. problem button save until forth textview, fifth , sixth not saved , copy text in third , fourth textviews
example
first text view - correctly saved
second text view - correctly saved
third text view - correctly saved
fourth text view - correctly saved
fifth text view - copy of text on third text view
sixth text view - copy of text on fourth text view
save button.
it seems code ok , of text views linked , last 2 textviews still fail.
my code here:
@iboutlet weak var scrollview: uiscrollview! @iboutlet weak var labeluno: uilabel! @iboutlet weak var labeldos: uilabel! @iboutlet weak var primertextview: uitextview! @iboutlet weak var segundotextview: uitextview! @iboutlet weak var tercertextview: uitextview! @iboutlet weak var cuartotextview: uitextview! @iboutlet weak var quintotextview: uitextview! @iboutlet weak var sextotextview: uitextview! override func viewdidload() { super.viewdidload() scrollview.contentsize.height = 2000 labeluno.text = "¿qué significa el éxito para ti? , ¿significa ser millonario , ser famoso , contribuir de alguna manera ayudar muchas personas, ser un buen padre o madre de familia, ser el mejor en tu profesión??" primertextview.layer.borderwidth = 0.8 primertextview.layer.bordercolor = uicolor.blackcolor().cgcolor primertextview.layer.cornerradius = 8.0 segundotextview.layer.borderwidth = 0.8 segundotextview.layer.bordercolor = uicolor.blackcolor().cgcolor segundotextview.layer.cornerradius = 8.0 tercertextview.layer.borderwidth = 0.8 tercertextview.layer.bordercolor = uicolor.blackcolor().cgcolor tercertextview.layer.cornerradius = 8.0 cuartotextview.layer.borderwidth = 0.8 cuartotextview.layer.bordercolor = uicolor.blackcolor().cgcolor cuartotextview.layer.cornerradius = 8.0 quintotextview.layer.borderwidth = 0.8 quintotextview.layer.bordercolor = uicolor.blackcolor().cgcolor quintotextview.layer.cornerradius = 8.0 sextotextview.layer.borderwidth = 0.8 sextotextview.layer.bordercolor = uicolor.blackcolor().cgcolor sextotextview.layer.cornerradius = 8.0 //save button let stringkey = nsuserdefaults.standarduserdefaults() segundotextview.text = stringkey.stringforkey("key") primertextview.text = stringkey.stringforkey("key2") tercertextview.text = stringkey.stringforkey("key3") cuartotextview.text = stringkey.stringforkey("key4") quintotextview.text = stringkey.stringforkey("key5") sextotextview.text = stringkey.stringforkey("key6") //hide keyboard //hide keyboard touching anywhere var tap: uitapgesturerecognizer = uitapgesturerecognizer(target: self, action: "dismisskeyboard") view.addgesturerecognizer(tap) } func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() return true } @ibaction func savebutton(sender: anyobject) { let mytext = segundotextview.text let mytext2 = primertextview.text let mytext3 = tercertextview.text let mytext4 = cuartotextview.text let mytext5 = quintotextview.text let mytext6 = sextotextview.text nsuserdefaults.standarduserdefaults().setobject(mytext, forkey: "key2") nsuserdefaults.standarduserdefaults().setobject(mytext2, forkey: "key") nsuserdefaults.standarduserdefaults().setobject(mytext3, forkey: "key3") nsuserdefaults.standarduserdefaults().setobject(mytext4, forkey: "key4") nsuserdefaults.standarduserdefaults().setobject(mytext3, forkey: "key5") nsuserdefaults.standarduserdefaults().setobject(mytext4, forkey: "key6") nsuserdefaults.standarduserdefaults().synchronize() var alert = uialertcontroller(title: "guardado", message:"tu texto se ha guardado", preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "ok", style: .cancel, handler: nil)) self.presentviewcontroller(alert, animated: true, completion: nil) } //hide keyboard return button func textview(textview: uitextview, shouldchangetextinrange range: nsrange, replacementtext text: string) -> bool { if text == "\n" { primertextview.resignfirstresponder() segundotextview.resignfirstresponder() tercertextview.resignfirstresponder() cuartotextview.resignfirstresponder() quintotextview.resignfirstresponder() sextotextview.resignfirstresponder() return false } return true } //hide keyboard touching anywhere func dismisskeyboard(){ view.endediting(true) }
}
i guess in part:
nsuserdefaults.standarduserdefaults().setobject(mytext, forkey: "key2") nsuserdefaults.standarduserdefaults().setobject(mytext2, forkey: "key") nsuserdefaults.standarduserdefaults().setobject(mytext3, forkey: "key3") nsuserdefaults.standarduserdefaults().setobject(mytext4, forkey: "key4") nsuserdefaults.standarduserdefaults().setobject(mytext3, forkey: "key5") nsuserdefaults.standarduserdefaults().setobject(mytext4, forkey: "key6")
it should be:
nsuserdefaults.standarduserdefaults().setobject(mytext2, forkey: "key2") nsuserdefaults.standarduserdefaults().setobject(mytext, forkey: "key") nsuserdefaults.standarduserdefaults().setobject(mytext3, forkey: "key3") nsuserdefaults.standarduserdefaults().setobject(mytext4, forkey: "key4") nsuserdefaults.standarduserdefaults().setobject(mytext5, forkey: "key5") nsuserdefaults.standarduserdefaults().setobject(mytext6, forkey: "key6")
it looks copy/paste error: put mytext5
, mytext6
instead of mytext3
, mytext4
in 2 last lines.
Comments
Post a Comment