私は自分のアプリでコアデータを使用しており、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];
}
私はいくつかの助けが大好きです!!!