我让我的项目有效,并决定为用户引入一个设置页面进行基本调整。
Entity name: "Setting"
Attribute: "settingTimeOverrideSlider"
-> I created the NSManagedObject Subclass
我添加了(并嵌入)了SettingsViewController,即segue,命名为segue。
在该页面上,我使用按钮保存更改。这个按钮在我的CoreViewController中通过[super saveAndDismiss]
调用了一个方法,我知道它进入那里(用NSLog检查)。
以下是方法:
-(void)saveAndDismiss
{
NSLog(@"In Save And Dismiss");
NSError *error = nil;
if ([self.managedObjectContext hasChanges]) {
NSLog(@"In Save And 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];
}
但由于某种原因,它永远不会进入第一个if()
,我的下一个调试NSLog。
我相信我的问题是我的主页中的prepareForSegue
,因为所有其他的segues都会转到tableViews。我不确定我的segue设置是否正确
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell*)sender
{
// segues 1-3
if ([[segue identifier]isEqualToString:@"segue4"])
{
// some 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;
}
}
在我的SettingsViewController.h中,我将我的SettingsViewController子类化为我的CoreViewController的一部分,如下所示:
// 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;
现在,当我尝试从持久性内存中检索信息时,是否需要将上述语句更改为
@interface SettingsViewController : CoreViewController
<NSFetchedResultsControllerDelegate>
//SettingsViewController.m
#import "SettingsViewController.h"
#import "AppDelegate.h"
@interface SettingsViewController ()
@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong)NSFetchedResultsController *fetchedResultsController;
似乎无法解决这里的错误,但我确定它在我的prepareForSegue
方法中(如上所列)。
就像我说的那样,所有其他的segue if语句用于链接到TableViews并且工作正常。
我试图像这样设置我的属性
editSetting.settingTimeOverrideSlider = @YES;
然后我打电话
[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; // THIS IS WHAT SHOULD CHANGE IN CORE DATA
[super saveAndDismiss];
}
}
答案 0 :(得分:0)
您不应将fetchedResultsController
或managedObjectContext
发送到SettingsViewController。您应该尽可能限制需要配置的信息量。您已经发送了editSetting
,这足以获得您想要的结果。
在SettingsViewController
中,删除以下两行:
@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong)NSFetchedResultsController *fetchedResultsController;
相反,您应该直接从NSManagedObject访问托管对象上下文。
您的saveAndDismiss
现在应该是这样的:
-(void)saveAndDismiss
{
NSLog(@"In Save And Dismiss");
NSError *error = nil;
if ([self.editSetting.managedObjectContext hasChanges]) {
NSLog(@"In Save And 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];
}
注意self.managedObjectContext
成为self.editSetting.managedObjectContext
的方式。
更好的是,您可以将[self.editSetting.managedObjectContext hasChanges]
更改为[self.editSetting isUpdated]
。