我不確定是否採取了正確的方法,但是我想從不同的viewControllers訪問購物車類的模型。我的第一種方法是在每個viewController中初始化一個Cart *對象,但是我想我只是想結束多個購物車對象,只要我想要的就是可以全局訪問的對象。搜索後,我想出了一種更好的方法,但沒有成功。如何從不同的viewControllers訪問模型對象
我有一個按鈕,應該增加一個購物車的交易。但是當我嘗試添加它時,該方法沒有被調用。這是我如何設置的。
在我的Cart類中,我有一個NSMutableArray來保存我的交易。
在我的viewController我設置類型車*車的屬性,這樣
@property (strong, nonatomic) Cart *cart;
初始化...
-(id)initWithModel:(Cart *)cart {
self = [super init];
if(self){
self.cart = cart;
}
return self;
}
然後我的按鈕的方法是這樣
-(IBAction)addDealToCart {
NSLog(@"The Cart has %i items", [self.cart qtyOfItemsInCart]);
NSLog(@"Added the Deal to the Cart");
[self.cart addDealsToCart:self.deal];
NSLog(@"The Cart now has %i items", [self.cart qtyOfItemsInCart]);
self.deal.qtyInCart = self.deal.qtyInCart + 1;
NSLog(@"the deal has %i items in the Cart", self.deal.qtyInCart);
}
在這一行addDealsToCart:交易永遠不會被調用。
這裏是我的addDealsToCart:在我的車類
-(void)addDealsToCart:(Deals *)deal {
[self.cartContents addObject:deal];
NSLog(@"the deal was added to the cart %@",deal);
}
任何幫助將是巨大的交易方法。謝謝
當你運行你的代碼時,'NSLog(@「該交易被添加到購物車%@」,交易);'打印? 'NSLog同樣的問題(@「將交易加入購物車」);'? – Barjavel 2013-04-07 01:23:42
@Barjavel否,'NSLog(@「該交易被添加到購物車%@」,交易);'沒有被調用。這個'NSLog(@「將交易加入購物車」);' – vinylDeveloper 2013-04-07 01:35:33
似乎'self.cart'可能是'nil'那麼。 – Barjavel 2013-04-07 08:36:37