2013-04-24 1 views
0

내 앱에서 핵심 데이터를 사용하고 있는데 내 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을 내 대리자의 것과 비교할 수 있습니다.

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 

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

} 

내가 도움을 사랑합니다!

답변

2

당신은 그것이 AppDelegate에 자신을 형성 얻을 수 :

#import "AppDelegate.h" 

//... 

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

을 여기에 내가, 당신이 만든 하나 개 AppDelegate에있는 상황, 그리고 Addviewcontroller에서 하나를 참조합니다. 여기서 저장소와 스레드는 하나뿐이므로 AddviewController에서 관리 대상 개체 컨텍스트의 속성을 제거해야합니다. 은 또한 다음과 같은 코드에 의해 addViewController 대리인으로부터 managedobjectcontext 할당해야

{ AppDelegate에의 위임 * = [UIApplication sharedApplication] .delegate; NSManagedObjectContext * addViewcontrollerLocalContext = delegate.managedObjectContext; }

이렇게하면 모든 개체가 AppDelegate 파일에서 만든 MainObjectContext에 저장됩니다.