I have this managedContextObject I want to pass from a view controller to another. From a view controller called CatalogueViewController this works fine with no problem. And this is the no-problem code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"AddItem"]) {
UINavigationController *navigationController = segue.destinationViewController;
DetailsItemViewController *detailsItemViewController = (DetailsItemViewController *) navigationController.topViewController;
detailsItemViewController.delegate = self;
detailsItemViewController.productToAdd = sender; //Mando il prodotto che ha provocato la segue (fatto manulamente sopra in didSelectRowAtIndexPath).
detailsItemViewController.index = [prodotti.productsArray indexOfObject:sender];
detailsItemViewController.managedObjectContext = self.managedObjectContext; //Gli passo anche l'oggetto per registrare i prodotti aggiunti in core data.
NSLog(@"::::::::::::INDICE DELL'OGGETTO: %d", [prodotti.productsArray indexOfObject:sender]);
//delegato, vado ad aggiungere i metodi delegati
}
}
And the line detailsItemViewController.managedObjectContext = self.managedObjectContext; has no problem.
BUT! When I try to pass SAME THING to THE SAME detailsItemViewController (the only difference is that I do that from another view controller called CartViewController) and this is the code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"EditItem"]) {
UINavigationController *navigationController = segue.destinationViewController;
DetailsItemViewController *detailsItemViewController = (DetailsItemViewController *) navigationController.topViewController;
detailsItemViewController.productToEdit = sender; //Mando il prodotto che ha provocato la segue (fatto manulamente sopra in didSelectRowAtIndexPath.
detailsItemViewController.managedObjectContext = self.managedObjectContext; //Gli passo anche l'oggetto per registrare i prodotti aggiunti in core data.
//Mi metto in ascolto di una notifica tramite il Notification Center.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(objectEditedFromDetailsViewController:)
name:@"ObjectEdited"
object:nil];
}
}
on the line: detailsItemViewController.managedObjectContext = self.managedObjectContext; Xcode is giving me this error message:
[…]CartViewController.m: error: Semantic Issue: Property ‘managedObjectContext’ not found on object of type ‘DetailsItemViewController *’
that obviously is not true!
WHY?!
Just to be clearer:
I make the #import of DetailsItemViewController.h in CartViewController:
#import "CartViewController.h"
#import "Product.h"
#import "CartCell.h"
#import "CDProduct.h"
#import "DetailsItemViewController.h"
#import "UIImage+Resize.h"
@implementation CartViewController {
All the lines but detailsItemViewController.managedObjectContext = self.managedObjectContext; work. All this lines work with no problem at all:
UINavigationController *navigationController = segue.destinationViewController; // <---- WORKS
DetailsItemViewController *detailsItemViewController = (DetailsItemViewController *) navigationController.topViewController; // <---- WORKS
detailsItemViewController.productToEdit = sender; // <---- WORKS
In fact, for example, if I try to pick up another detailsItemViewController’s property it works well! Only if I do
detailsItemViewController.managedObjectContext = self.managedObjectContext; I get the error from Xcode.
And here the DetailsItemViewController.h
#import <UIKit/UIKit.h>
#import "Product.h"
#import "ProductDetails.h"
#import "CDProduct.h"
//Delegato per lo screen successivo di aggiunta roba nel carrello.
@class DetailsItemViewController;
@class CatalogueItem;
@protocol DetailsItemViewControllerDelegate <NSObject>
- (void)detailsViewControllerDidCancel:(DetailsItemViewController *)controller;
- (void)detailsViewControllerDidDone:(DetailsItemViewController *)controller didFinishAddingItem:(CatalogueItem *)item;
@end
//@class Product;
//@class ProductDetails;
@interface DetailsItemViewController : UIViewController <NSURLConnectionDelegate>
@property (nonatomic, weak) id <DetailsItemViewControllerDelegate> delegate;
@property (nonatomic, strong) Product *productToAdd; //Li differenzio così capisco quello che devo fare. NB: per chi legge questo codice:productToAdd non indica un oggetto da aggiungere ma un oggetto a cui si può modificare la quantità per aggiungerlo al carrello.
@property (nonatomic, strong) CDProduct *productToEdit;
@property (nonatomic, strong) ProductDetails *productToShow; //Qui ci metto il prodotto che ricavo da loadProducts.
@property (strong, nonatomic) IBOutlet UIImageView *graphicImage;
@property (strong, nonatomic) IBOutlet UIImageView *overviewImage;
@property (strong, nonatomic) IBOutlet UIStepper *stepper;
@property (strong, nonatomic) IBOutlet UILabel *stepperValueLabel;
@property (strong, nonatomic) IBOutlet UILabel *productNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *priceLabel; //Il prezzo poi lo prendo facendo il parsing di un altra pagina.
@property (strong, nonatomic) IBOutlet UILabel *totalPrice;
@property (nonatomic, assign) int index; //In questa property ci metto l'indice dell'oggetto passato (productToAdd) in modo sapere che articolo dell'XML parsare.
@property (nonatomic, strong) NSMutableData *receivedData;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; //Per registrare i prodotti in core data.
- (IBAction)cancel;
- (IBAction)done;
- (IBAction)changeValue:(UIStepper *)sender;
@end
Just solved the problem!
Well this wasn’t a simple one. It happened that few days earlier i had to redo the project (because i have accidentally erased the Storyboard), and so i added some classes from the old to the new same named project. The problem is that Xcode shuffles all the classes in different folder so i had some copied class in the new project folder and other older class in subfolder. This cause me to have class with the same name here and there but with different code. So, EVEN if in the Xcode IDE i had the new detailsViewController WITH managedContextObject, the CartViewController was referring to an old version of detailsViewController that has not the managedContextObject property (but has all the other property because i have created them before the mess with the storyboard).
So if you want to copy some older class in your project pay a lot of attention in what you are doing.