0

For some reason my instance variable (in my viewcontroller) is returning null in viewDidAppear but its returning the correct value in viewDidLoad..

- (void)viewDidLoad {
    [super viewDidLoad];

        NSLog(@"viewDidLoad: %@",self.product.sku);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
            NSLog(@"viewDidAppear: %@",self.product.sku);

    [self adjustViews];
}

This only happens when i'm loading my viewController from my appdelegate like below:

ProductDetailViewController *controller = [[ProductDetailViewController alloc] initWithProduct:product];

[(UINavigationController *)self.tabBarController.selectedViewController pushViewController:controller animated:YES];

If i access my controller through other screen it works fine...

DProduct *product = [self.resultsController objectAtIndexPath:indexPath];
ProductDetailViewController *detailViewController = [[ProductDetailViewController alloc] initWithProduct:product];
[self.navigationController pushViewController:detailViewController animated:YES];

initWithProduct function

- (id)initWithProduct:(DProduct *)product {
    self = [super init];
    if (self) {
        self.product = product;
        self.title = product.sku;
    }
    NSLog(@"initwithproduct: %@",self.product.sku);

    return self;
}

setProduct function

- (void)setProduct:(DProduct *)product {
    NSLog(@"SET PRODUCT WAS CALLED...%@",product.sku);
    product_ = product;
    if (product) {
        [self.cartButton removeFromSuperview];
        BOOL outOfStock = [product.stock unsignedIntegerValue] == 0;
        NSString *title = outOfStock ? NSLocalizedString(@"Out of Stock", nil) : NSLocalizedString(@"Add To Cart", nil);
        ThemedButton *cartButton = [ThemedButton buttonWithTitle:title style:outOfStock ? ThemedButtonStyleRed : ThemedButtonStylePink];
        [cartButton addTarget:nil action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];
        [cartButton sizeToFit];
        cartButton.enabled = !outOfStock;
        [self addSubview:cartButton];
        self.cartButton = cartButton;
        [self setupInterface];
    }
}

Declarations for product and sku

   @interface ProductDetailViewController()
        @property (nonatomic, strong) ProductDetailView *detailView;
        @property (nonatomic, strong) UIScrollView *scrollView;
        @property (nonatomic, strong) DProduct *product;
        @property (nonatomic, assign) BOOL keyboardIsShown;

        - (void)configureDetailView;
        - (void)adjustViews;
        - (NSURL *)productURL;
    @end

    @implementation ProductDetailViewController

    @synthesize detailView = detailView_;
    @synthesize scrollView = scrollView_;
    @synthesize keyboardIsShown = keyboardIsShown_;
    @synthesize product = product_;

    @interface DProduct : DAsset

@property (nonatomic, retain) NSNumber * available;
@property (nonatomic, retain) NSString * detail;
@property (nonatomic, retain) NSNumber * price;
@property (nonatomic, retain) NSNumber * shipping;
@property (nonatomic, retain) NSString * sku;
Parampal Pooni
  • 2,958
  • 8
  • 34
  • 40
  • I'd (a) look at the `product` variable before the invocation of `initWithProduct` in both scenarios, and (b) can you show us what `initWithProduct` does with the `product` you pass to it (e.g. simple assignment to ivar? `copy` of it? etc.). Bottom line, it's either `product` var before `initWithProduct` or that `initWithProduct` is not retaining it. But we don't have enough here to determine. – Rob May 29 '13 at 01:42
  • ARC? Show us the declarations for product and sku. (Note that in the second case "product" remains retained by resultsController.) – Hot Licks May 29 '13 at 01:46
  • ohhh.. how do i fix that? ive updated the question.. – Parampal Pooni May 29 '13 at 01:55
  • ARC??? Declarations??? (But it appears that you DO NOT retain product, so it may be going poof.) – Hot Licks May 29 '13 at 02:59
  • ive added the declarations.. yep i think its using ARC – Parampal Pooni May 29 '13 at 03:50
  • Rob: the product var is the same in both scenarios, so im guessing its case (b) – Parampal Pooni May 29 '13 at 05:34
  • im getting the following output when retrieving the product - im not sure why the data is : (entity: DProduct; id: 0xbc876a0 ; data: ) – Parampal Pooni May 30 '13 at 06:39

1 Answers1

0

The way to find such things is to switch from an ivar to a property, write your own setter, then add a nslog if the new value is nil. Put a breakpoint on the log message and you will discover how it's getting nilled.

David H
  • 40,852
  • 12
  • 92
  • 138
  • im getting the following output when retrieving the product - im not sure why the data is : (entity: DProduct; id: 0xbc876a0 ; data: ) – Parampal Pooni May 30 '13 at 05:35
  • So your problem now is a quite complex interaction between Core Data, your view controllers, and who knows what. Somehow you need to peel the onion until you find what the problem is. If this was me, I'd be adding log messages all over the place to try and determine when things start going wrong - and adding lots of asserts too. – David H May 30 '13 at 11:05