2014-04-12 7 views
0

У меня возникли проблемы с моим приложением. В методе viewDidLoad объект _recipe распечатает свои значения. Однако, когда метод addToFavourites называется значениями _recipe, объект возвращает null, и я не могу понять, почему любая помощь будет оценена по достоинству.Значения NSManagedObject неожиданно меняются null

Heres the class.

#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

Смежные вопросы