由于某种原因,我的实例变量(在我的视图控制器中)在 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;
}
设置产品功能
- (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;