在AppDelegate中将对象传递给NSMutableArray

时间:2013-07-09 16:36:41

标签: ios objective-c cocoa-touch nsmutablearray

我在我的应用中制作购物车的概念时遇到了麻烦。我有我的AppDelegate(名为ST2AppDelegate),其中包含一个名为myCart的NSMutableArray。我希望RecipeViewController.m将NSString对象传递给myCart,但每次我传递NSString并使用NSLog来显示数组的内容时,它总是为空。

有谁能告诉我我做错了什么?我已经处理了这段代码了好几天,并且有一行代码,我根本不理解发生了什么(在RecipeViewController.m中,标记为这样)。

任何帮助都会非常感激......我只是一个初学者。以下是相关课程:

ST2AppDelegate.h:

#import <UIKit/UIKit.h>

@interface ST2AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSMutableArray* myCart;

- (void)addToCart:(NSString*)item;
- (void)readCartContents;

@end

ST2AppDelegate.m:

#import "ST2AppDelegate.h"

@implementation ST2AppDelegate

@synthesize myCart;

// all the 'applicationDid...' methods...

- (void)addToCart:(NSString *)item
{
    [self.myCart addObject:item];
}

- (void)readCartContents
{
    NSLog(@"Contents of cart: ");
    int count = [myCart count];
    for (int i = 0; i < count; i++)
    {
        NSLog(@"%@", myCart[count]);
    }
}

@end

RecipeDetailViewController.h:

#import <UIKit/UIKit.h>
#import "ST2AppDelegate.h"

@interface RecipeDetailViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UILabel* recipeLabel;
@property (nonatomic, strong) NSString* recipeName;
@property (nonatomic, strong) IBOutlet UIButton* orderNowButton;

- (IBAction)orderNowButtonPress:(id)sender;

@end

RecipeDetailViewController.m:

#import "RecipeDetailViewController.h"

@implementation RecipeDetailViewController

@synthesize recipeName;
@synthesize orderNowButton;

// irrelevant methods...

- (IBAction)orderNowButtonPress:(id)sender
{
    // alter selected state
    [orderNowButton setSelected:YES];
    NSString* addedToCartString = [NSString stringWithFormat:@"%@ added to cart!",recipeName];
    [orderNowButton setTitle:addedToCartString forState:UIControlStateSelected];

    // show an alert
    NSString* addedToCartAlertMessage = [NSString stringWithFormat:@"%@ has been added to your cart.", recipeName];
    UIAlertView* addedToCartAlert = [[UIAlertView alloc] initWithTitle:@"Cart Updated" message:addedToCartAlertMessage  delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [addedToCartAlert show];

    // add to cart (I don't understand this, but it works)
    [((ST2AppDelegate*)[UIApplication sharedApplication].delegate) addToCart:recipeName];

    // read cart contents
    [((ST2AppDelegate*)[UIApplication sharedApplication].delegate) readCartContents];
}

@end

2 个答案:

答案 0 :(得分:2)

您需要在应用程序启动时初始化myCart:

self.myCart = [[NSMutableArray alloc] init];

否则你只是试图将对象添加到一个nil对象,虽然它不会抛出异常,因为objective-c处理nil对象的方式在初始化之前它不会按预期运行。

答案 1 :(得分:1)

您是否曾购买过购物车变量?

尝试进行延迟实例化。

-(NSMutableArray *) myCart{
     if (!_myCart){
          _myCart = [[NSMutableArray alloc] init];
     }
   return _myCart;

}

通过这种方式,您将知道它将始终被分配。基本上,这种方法使得无论何时有人调用对象的类版本,它都会检查该对象是否已被分配,如果没有,则分配它。这是您应该使用大多数对象的常见范例。

此方法应该放在app委托中(声明对象的位置)。