我在我的应用程序上使用核心数据,我知道我需要连接我的managedObjectContext
throw 委托,但我不知道如何......我有一个委托 -
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (strong, nonatomic) UINavigationController *nav;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "mainViewController.h"
#import "addViewController.h"
@implementation AppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize nav;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//----------------------------------nav-------------------------------------------------
mainViewController*mainView=[[mainViewController alloc]init];
nav=[[UINavigationController alloc]initWithRootViewController:mainView];
mainView.manageObjectContext=self.managedObjectContext;
[self.window addSubview:nav.view];
//----------------------------------nav-end---------------------------------------------
[self.window makeKeyAndVisible];
return YES;
}
MainVeiw 不是我要保存实体的页面(所以我无法匹配委托本身的对象),这是另一个页面,我认为我的问题是我不知道如何导入委托 id 对象所以我可以与managedObjectContext
我的委托人进行比较。
addViewController.h:
#import <UIKit/UIKit.h>
@interface addViewController : UIViewController <UIPickerViewAccessibilityDelegate,UIPickerViewDataSource,UITextFieldDelegate>
{
NSDictionary *allSubjects;
NSArray* arrSubject;
NSArray* arrSubSubjects;
NSManagedObjectContext*manageObjectContext;
}
@property(nonatomic,strong)NSManagedObjectContext*manageObjectContext;
//@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
//@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (strong, nonatomic) NSDictionary *allSubjects;
@property (strong, nonatomic) NSArray* arrSubject;
@property (strong, nonatomic) NSArray* arrSubSubjects;
@property (weak, nonatomic) IBOutlet UIScrollView *scrolladdview;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerSubjects;
@property (weak, nonatomic) IBOutlet UITextField *txtDesc;
@property (weak, nonatomic) IBOutlet UITextField *txtUserName;
@property (weak, nonatomic) IBOutlet UITextField *txtPassword;
-(IBAction)createPassword:(id)sender;
@end
添加视图控制器.m
#import "addViewController.h"
@interface addViewController ()
@end
@implementation addViewController
@synthesize allSubjects,arrSubject,arrSubSubjects,pickerSubjects;
@synthesize txtDesc,txtPassword,txtUserName,scrolladdview,manageObjectContext;
-(IBAction)createPassword:(id)sender
{
NSInteger row;
NSString*subTypeSelectd=[[NSString alloc]init];
row = [pickerSubjects selectedRowInComponent:0];
subTypeSelectd = [arrSubSubjects objectAtIndex:row];
NSInteger row2;
NSString*TypeSelectd=[[NSString alloc]init];
row2 = [pickerSubjects selectedRowInComponent:1];
TypeSelectd = [arrSubSubjects objectAtIndex:row2];
//here is the error: no visible @interface for 'addViewController' declares the selector 'NSManagedObject'
NSManagedObject*password=[NSEntityDescription insertNewObjectForEntityForName:@"Password" inManagedObjectContext:[self managedObjectContext]];
[password setValue:self.txtDesc.text forKey:@"desc"];
[password setValue:self.txtUserName.text forKey:@"userName"];
[password setValue:self.txtPassword.text forKey:@"password"];
[password setValue:subTypeSelectd forKey:@"subType"];
[password setValue:TypeSelectd forKey:@"type"];
NSError*error;
if(![[self manageObjectContext]save:&error])
NSLog(@"input %@",error);
else NSLog(@"saved");
[self.navigationController popViewControllerAnimated:YES];
}
我会喜欢一些帮助!