ios - Doesn't save editing text in UITextView -


i have viewcontrollers , 2 classes masterviewcontroller.swift , loginviewcontroller.swift. in first class have tableview in can add record , open new viewcontroller class detailviewcontroller.swift in textview in can add text , when go tableview must save text, when it doesn't save. cod masterviewcontroller:

import uikit import coredata  class masterviewcontroller: uiviewcontroller, uitableviewdatasource,  uitableviewdelegate, nsfetchedresultscontrollerdelegate {      @iboutlet var tableview: uitableview!      var isauthenticated = false      var managedobjectcontext: nsmanagedobjectcontext? = nil     var _fetchedresultscontroller: nsfetchedresultscontroller? = nil      var didreturnfrombackground = false       override func awakefromnib() {         super.awakefromnib()     }      override func viewdidload() {         super.viewdidload()         self.navigationitem.leftbarbuttonitem = self.editbuttonitem()         view.alpha = 0          let addbutton = uibarbuttonitem(barbuttonsystemitem: .add, target: self, action: "insertnewobject:")         self.navigationitem.rightbarbuttonitem = addbutton          nsnotificationcenter.defaultcenter().addobserver(self, selector: "appwillresignactive:", name: uiapplicationwillresignactivenotification, object:   nil)          nsnotificationcenter.defaultcenter().addobserver(self, selector: "appdidbecomeactive:", name: uiapplicationdidbecomeactivenotification, object: nil)     }      @ibaction func unwindsegue(segue: uistoryboardsegue) {          isauthenticated = true         view.alpha = 1.0     }      func appwillresignactive(notification : nsnotification) {          view.alpha = 0         isauthenticated = false         didreturnfrombackground = true     }      func appdidbecomeactive(notification : nsnotification) {          if didreturnfrombackground {             self.showloginview()         }     }       override func viewdidappear(animated: bool) {         super.viewdidappear(false)         self.showloginview()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()     }      func showloginview() {          if !isauthenticated {              self.performseguewithidentifier("loginview", sender: self)         }     }      func insertnewobject(sender: anyobject) {         let context = self.fetchedresultscontroller.managedobjectcontext         let entity = self.fetchedresultscontroller.fetchrequest.entity!         let newmanagedobject = nsentitydescription.insertnewobjectforentityforname(entity.name!, inmanagedobjectcontext: context) as! nsmanagedobject          newmanagedobject.setvalue(nsdate(), forkey: "date")         newmanagedobject.setvalue("new note", forkey: "notetext")          var error: nserror? = nil         if !context.save(&error) {             abort()         }     }      override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         if segue.identifier == "showdetail" {             if let indexpath = self.tableview.indexpathforselectedrow() {                 let object = self.fetchedresultscontroller.objectatindexpath(indexpath)as! nsmanagedobject                 (segue.destinationviewcontroller as! detailviewcontroller).detailitem = object             }         }     }      func numberofsectionsintableview(tableview: uitableview) -> int {         return self.fetchedresultscontroller.sections?.count ?? 0     }      func tableview(tableview: uitableview, numberofrowsinsection section: int) ->  int {         let sectioninfo = self.fetchedresultscontroller.sections![section] as! nsfetchedresultssectioninfo         return sectioninfo.numberofobjects     }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell         self.configurecell(cell, atindexpath: indexpath)         return cell     }      func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool {          return true     }      func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {         if editingstyle == .delete {             let context = self.fetchedresultscontroller.managedobjectcontext             context.deleteobject(self.fetchedresultscontroller.objectatindexpath(indexpath) as! nsmanagedobject)              var error: nserror? = nil             if !context.save(&error) {                 abort()             }         }     }      func configurecell(cell: uitableviewcell, atindexpath indexpath: nsindexpath) {         let object = self.fetchedresultscontroller.objectatindexpath(indexpath) as! nsmanagedobject         cell.textlabel?.text = object.valueforkey("notetext")!.description     }       @ibaction func logoutaction(sender: anyobject) {          isauthenticated = false         self.performseguewithidentifier("loginview", sender: self)     }       var fetchedresultscontroller: nsfetchedresultscontroller {         if _fetchedresultscontroller != nil {             return _fetchedresultscontroller!         }          let fetchrequest = nsfetchrequest()          let entity = nsentitydescription.entityforname("note", inmanagedobjectcontext: self.managedobjectcontext!)         fetchrequest.entity = entity          fetchrequest.fetchbatchsize = 20          let sortdescriptor = nssortdescriptor(key: "date", ascending: false)         let sortdescriptors = [sortdescriptor]          fetchrequest.sortdescriptors = [sortdescriptor]          let afetchedresultscontroller = nsfetchedresultscontroller(fetchrequest: fetchrequest, managedobjectcontext: self.managedobjectcontext!,        sectionnamekeypath: nil, cachename: "master")         afetchedresultscontroller.delegate = self         _fetchedresultscontroller = afetchedresultscontroller          var error: nserror? = nil         if !_fetchedresultscontroller!.performfetch(&error) {              abort()         }          return _fetchedresultscontroller!      }       func controller(controller: nsfetchedresultscontroller, didchangesection sectioninfo: nsfetchedresultssectioninfo, atindex sectionindex: int,   forchangetype type: nsfetchedresultschangetype) {         switch type {             case .insert:                 self.tableview.insertsections(nsindexset(index: sectionindex), withrowanimation: .fade)             case .delete:                 self.tableview.deletesections(nsindexset(index: sectionindex), withrowanimation: .fade)             default:                 return         }     }      func controller(controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) {         switch type {             case .insert:                 tableview.insertrowsatindexpaths([newindexpath!], withrowanimation: .fade)             case .delete:                 tableview.deleterowsatindexpaths([indexpath!], withrowanimation: .fade)             case .update:                 self.configurecell(tableview.cellforrowatindexpath(indexpath!)!, atindexpath: indexpath!)             case .move:                 tableview.deleterowsatindexpaths([indexpath!], withrowanimation: .fade)                 tableview.insertrowsatindexpaths([newindexpath!], withrowanimation: .fade)             default:                 return         }     }   } 

my detailviewcontrooler.swift:

import uikit import coredata  class detailviewcontroller: uiviewcontroller, uitextviewdelegate {      let managedobjectcontext = (uiapplication.sharedapplication().delegate as! appdelegate)     @iboutlet weak var detailtextview: uitextview!      var note: note? = nil      var detailitem: anyobject? {         didset {             self.configureview()         }     }      func configureview() {          if let detail: note = self.detailitem as? note {             if let detailtextview = self.detailtextview {                 detailtextview.text = detail.notetext             }         }     }      override func viewdidload() {         super.viewdidload()         self.configureview()     }      func textviewdidendediting( textview: uitextview) {          if let detail: note = self.detailitem as? note {             if let detailtextview = self.detailtextview {                 detail.notetext = detailtextview.text             }         }         managedobjectcontext.managedobjectcontext!.save(nil)      } } 


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -