ios - Core Data segue issue with saving changes -
i had project working, , decided introduce settings page users make basic adjustments.
entity name: "setting" attribute: "settingtimeoverrideslider" -> created nsmanagedobject subclass
i added (and embedded) settingsviewcontroller, segue, named segue.
on page i'm using button save changes. button called method in coreviewcontroller via [super saveanddismiss]
, know gets in there (checked nslog).
here's method:
-(void)saveanddismiss { nslog(@"in save , dismiss"); nserror *error = nil; if ([self.managedobjectcontext haschanges]) { nslog(@"in save , dismiss - has changes"); if (![self.managedobjectcontext save:&error]) { // save failed nslog(@"save failed: %@", [error localizeddescription]); } else { // save succeeded nslog(@"save succeeded"); } } [self dismissviewcontrolleranimated:yes completion:nil]; }
but reason, never gets first if()
, have next debug nslog.
i believe problem prepareforsegue
in main page, other segues going tableviews. i'm not sure if have segue setup right
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(uitableviewcell*)sender { // segues 1-3 if ([[segue identifier]isequaltostring:@"segue4"]) { // other segue } if ([[segue identifier]isequaltostring:@"tosettings"]) { uinavigationcontroller *settingsviewcontroller = segue.destinationviewcontroller; settingsviewcontroller *editsettingsviewcontroller = (settingsviewcontroller*)settingsviewcontroller.topviewcontroller; nsindexpath *indexpath = [self.fetchedresultscontroller objectatindexpath:0]; setting *editsettings = (setting*)[self.fetchedresultscontroller objectatindexpath:indexpath]; editsettingsviewcontroller.editsetting = editsettings; } }
in settingsviewcontroller.h, subclass settingsviewcontroller part of coreviewcontroller so:
// settingsviewcontroller.h #import <uikit/uikit.h> #import "setting.h" #import "coreviewcontroller.h" @interface settingsviewcontroller : coreviewcontroller @property (nonatomic, strong)setting editsetting; @property (nonatomic, strong)setting *editsetting; @property (strong, nonatomic) iboutlet uibarbuttonitem *editsavebutton; @property (strong, nonatomic) iboutlet uitextfield *timeoverridetextlabel; @property (strong, nonatomic) iboutlet uisegmentedcontrol *timeformatselector; @property (strong, nonatomic) iboutlet uiswitch *timeoverrideslider; - (ibaction)cancel:(uibarbuttonitem *)sender; - (ibaction)editsave:(uibarbuttonitem *)sender; - (ibaction)timeoverride:(uiswitch *)sender;
now i'm trying retrieve information persistent memory, need change above statement to
@interface settingsviewcontroller : coreviewcontroller <nsfetchedresultscontrollerdelegate>
?
//settingsviewcontroller.m #import "settingsviewcontroller.h" #import "appdelegate.h" @interface settingsviewcontroller () @property (nonatomic, strong)nsmanagedobjectcontext *managedobjectcontext; @property (nonatomic, strong)nsfetchedresultscontroller *fetchedresultscontroller;
can't seem wrap head around what's wrong here, i'm sure in prepareforsegue
method (listed above).
like said, other segue if-statement used link tableviews , work fine.
i'm trying set attribute this
editsetting.settingtimeoverrideslider = @yes;
and call
[super saveanddismiss]; - (ibaction)editsave:(uibarbuttonitem *)sender { if ([_editsavebutton.title isequaltostring:@"edit"]) { [self settitle:@"edit settings"]; _timeformatselector.userinteractionenabled = yes; _timeoverrideslider.userinteractionenabled = yes; _editsavebutton.title = @"save"; } else if ([_editsavebutton.title isequaltostring:@"save"]) { [self settitle:@"settings"]; nslog(@"saving-svc"); editsetting.settingtimeoverrideslider = @yes; // should change in core data [super saveanddismiss]; } }
you shouldn't send in fetchedresultscontroller
nor managedobjectcontext
settingsviewcontroller. should limit amount of information needs configured possible. sending in editsetting
, enough information achieve results want.
in settingsviewcontroller
, delete following 2 lines:
@property (nonatomic, strong)nsmanagedobjectcontext *managedobjectcontext; @property (nonatomic, strong)nsfetchedresultscontroller *fetchedresultscontroller;
instead, should access managed object context nsmanagedobject directly.
your saveanddismiss
should this:
-(void)saveanddismiss { nslog(@"in save , dismiss"); nserror *error = nil; if ([self.editsetting.managedobjectcontext haschanges]) { nslog(@"in save , dismiss - has changes"); if (![self.editsetting.managedobjectcontext save:&error]) { // save failed nslog(@"save failed: %@", [error localizeddescription]); } else { // save succeeded nslog(@"save succeeded"); } } [self dismissviewcontrolleranimated:yes completion:nil]; }
notice how self.managedobjectcontext
became self.editsetting.managedobjectcontext
.
better yet, can change [self.editsetting.managedobjectcontext haschanges]
[self.editsetting isupdated]
.
Comments
Post a Comment