实例变量在viewDidAppear中返回null但在viewDidLoad中很好

时间:2013-05-29 01:12:57

标签: ios uiviewcontroller viewdidload viewdidappear

由于某种原因,我的实例变量(在我的viewcontroller中)在viewDidAppear中返回null,但它在viewDidLoad中返回正确的值..

- (void)viewDidLoad {
    [super viewDidLoad];

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

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

    [self adjustViews];
}

这只发生在我从我的appdelegate加载我的viewController时,如下所示:

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

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

如果我通过其他屏幕访问我的控制器,它可以正常工作......

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

initWithProduct函数

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

    return self;
}

setProduct函数

- (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];
    }
}

产品和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;

1 个答案:

答案 0 :(得分:0)

找到这样的东西的方法是从ivar切换到属性,编写自己的setter,然后在新值为nil时添加nslog。在日志消息上放置一个断点,您将发现它是如何被填充的。