2011-11-25 5 views
9

私は、アプリケーションデリゲートからビューコントローラにコンテキストを渡す際に問題があります。 私はインターネット上で多くのチュートリアルを見つけました。すべては、didFinishLaunchingWithOptionsメソッドを使用してView Controllerを作成し、コンテキストプロパティを設定してプッシュすることをお勧めします。 私の問題は、私がストーリーボードを使いたいということです。ビューコントローラーは、アプリケーションデリゲートではなく、その中で作成され、プッシュされます。ストーリーボードでコアデータを使用するためにiOSでコンテキストを渡す

私は私のアプリデリゲートでこれを行うにしようとしました:私のビューコントローラで

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
//instantiate local context 
NSManagedObjectContext *context = [self managedObjectContext]; 
if (!context) 
{ 
    // Handle the error. 
    NSLog(@"Error: Context is null"); 
} 

//reference the view controller 
helloCoreDataViewController1_001 *rootViewController = [helloCoreDataViewController1_001 alloc]; 

// Pass the managed object context to the view controller 
rootViewController.managedObjectContext = context; 

return YES; 
} 

と、この:

@implementation helloCoreDataViewController1_001 

@synthesize name, address, phone, status, managedObjectContext; 
//.... 

- (IBAction)saveContact 
{ 
NSLog(@"name: %@",self.name.text); 
NSLog(@"address: %@",self.address.text); 
NSLog(@"phone: %@",self.phone.text); 

//Save the new instance of the contact entity 
Contact *contact = (Contact *)[NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:managedObjectContext]; 

[contact setContactName:[NSString stringWithFormat:@"%@",self.name.text]]; 
[contact setValue:self.address.text forKey:@"address"]; 
[contact setContactPhone:[NSString stringWithFormat:@"%@",self.phone.text]]; 

NSError *error = nil; 

if (![managedObjectContext save:&error]) 
{ 
    // Handle the error. 
    NSLog(@"error: %@", error.description); 
    self.status.text = @"Error: contact NOT saved"; 
} 
else 
    self.status.text = @"Contact saved"; 
} 

私はデバッグするとき、私は、アプリのデリゲートでそれを見ることができ、コンテキストが正しく設定され、ビューコントローラのプロパティも正常です。 しかし、私のsaveContactメソッドが呼び出されると、コンテキストは空です。

ご意見はありますか?ストーリーボードでビューコントローラにコンテキストを渡すにはどうすればよいですか?

答えて

9

を働いたあなたがでストーリーボードのrootviewcontrollerを得ることができます新しいものを割り当てるのではなく、

self.window.rootViewController 

にアクセスして、完了しました。

だからあなたのdidFinishLaunchingWithOptionsのものの2行は次のようになります。

helloCoreDataViewController1_001 *rootViewController = (helloCoreDataViewController1_001 *)self.window.rootViewController; 
rootViewController.managedObjectContext = context; 
-1

代わりのビューコントローラに管理対象obejectコンテキストを渡すappDelegateからのビューコントローラ上でそれを取得しよう:

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (fetchedResultsController != nil) { 
     return fetchedResultsController; 
    } 
if (managedObjectContext == nil) { 
    id appDelegate = (id)[[UIApplication sharedApplication] delegate]; 
    self.managedObjectContext = [appDelegate managedObjectContext]; 
    }... //finish your fetch request of course... 
} 

私と一緒に、それは美しく

+0

をこれは、あなたのクラス階層は非常に剛性になり、あなたのビューコントローラは現在、アプリのデリゲートを知っている必要があり、それに依存しています。私は個人的にビューを作成するときに私が割り当てたビューコントローラ上のプロパティを持つことを好みます。 –

+0

@Chrisあなたは正しいです。これは単なる別の方法です。私は別の文脈を持つ私のアプリの一つでこれを使うと考えました。これは単なる別のオプションです... – Farini

関連する問題