0

我在我的应用程序上使用核心数据,我知道我需要连接我的managedObjectContextthrow 委托,但我不知道如何......我有一个委托 -

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

}

我会喜欢一些帮助!

4

2 回答 2

2

您可以从 AppDelegate 本身获得它:

#import "AppDelegate.h"

//...

-(IBAction)createPassword:(id)sender
{
//...
NSManagedObjectContext* context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]). managedObjectContext;
//...
}
于 2013-04-24T08:21:51.127 回答
0

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

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

通过这样做,所有对象都将保存在您在 AppDelegate 文件中创建的 MainObjectContext 中。

于 2013-04-24T08:48:41.517 回答