无法将我的managedObjectContext从我的视图连接到我的Delegate

时间:2013-04-24 08:15:12

标签: ios core-data nsmanagedobjectcontext

我在我的应用程序上使用核心数据,我知道我需要连接我的managedObjectContext抛出代表,但我不知道如何... 我有一个代表 -

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与我的代表上的 #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 进行比较。

addViewController.h:

#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];

}

addViewController.m

{{1}}

我会喜欢一些帮助!!!

2 个答案:

答案 0 :(得分:2)

你可以从AppDelegate本身获得它:

#import "AppDelegate.h"

//...

-(IBAction)createPassword:(id)sender
{
//...
NSManagedObjectContext* context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]). managedObjectContext;
//...
}

答案 1 :(得分:0)

我在这里看到,你在AppDelegate中创建了一个上下文,在Addviewcontroller中创建了一个上下文。 因为您只有一个商店和一个线程,所以您应该从AddviewController中删除托管对象上下文的属性。 还应该通过以下代码从addViewController中的委托中分配managedobjectcontext:

{     AppDelegate * delegate = [UIApplication sharedApplication] .delegate;     NSManagedObjectContext * addViewcontrollerLocalContext = delegate.managedObjectContext; }

通过执行此操作,所有对象都将保存在您在AppDelegate文件中创建的MainObjectContext中。