2014-04-12 39 views
0

我的應用程序出現問題。在viewDidLoad方法中,_recipe對象將打印出其值。但是,當調用addToFavourites方法時,_recipe對象的值返回null,我無法弄清楚爲什麼有什麼幫助將得到主要讚賞。NSManagedObject的值突然變爲空

繼承人類。

#import "DetailViewController.h" 
    #import "Recipe.h" 
    #import "AppDelegate.h" 

    @interface DetailViewController() 

    @end 

    @implementation DetailViewController 

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
} 
    return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

    self.recipetitle.text = _recipe.title; 

    self.recipeingrediants.text = _recipe.ingrediants; 
    self.recipemethod.text = _recipe.method; 

    NSString *str = self.recipe.img64; 
NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:0]; 

self.recipeimage.image = [UIImage imageWithData:data]; 
    NSLog(@"This is odd.. %@",_recipe.title); 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (IBAction)addToFavourites:(id)sender { 

NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication]  delegate] managedObjectContext]; 
Recipe *favourite = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:context]; 


[favourite setValue:self.recipe.title forKey:@"title"]; 
[favourite setValue:_recipe.ingrediants forKey:@"ingrediants"]; 

NSLog(@"ERMM WHAT %@",_recipe.title); 
[favourite setValue:_recipe.method forKey:@"method"]; 
[favourite setValue:_recipe.img64 forKey:@"img64"]; 
[favourite setValue:_recipe.type forKey:@"type"]; 


NSError *err; 
[context save:&err]; 

NSLog(@"SAVE FIRED"); 

} 
@end 

回答

0

好的我已經知道了!對_recipe的提及是weak!因此,當viewDidLoad方法結束了以前的觀點堆放在堆上。由於_recipe沒有額外的引用計數,它也堆積如山!

+1

感謝您回來的解決方案。請儘快回覆您的回覆。 –

+0

@flexaddicted不用擔心!將在23小時內完成:) – bdavies6086