I made an NSMutableArray in my AppDelegate and I'm trying to add/get objects from one of my ViewControllers. I already made the viewController a sharedApplication so I know it's not that. I'm pretty sure it's a problem with the allocating of the NSMutableArray. I tried allocating it in this method of the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//sleep(3);
coupons = [[NSMutableArray alloc] init];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
Coupons being the NSMutableArray
But then my app doesn't load on my device! Where should I put the allocating code!?
i had also try and got success. my code is.
in AppDelegate.h
NSMutableArray *coupons;
@property (nonatomic , retain) NSMutableArray *coupons;
in AppDelegate.m
@synthesize coupons;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
coupons=[[NSMutableArray alloc] init];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
in viewcontroller.h #import AppDelegate.h and
@interface testViewController : UIViewController
{
testAppDelegate *appdeleate;
}
in viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
appdeleate=[[UIApplication sharedApplication]delegate];
[appdeleate.coupons addObject:@"one"];
[appdeleate.coupons addObject:@"two"];
[appdeleate.coupons addObject:@"three"];
[appdeleate.coupons addObject:@"fore"];
[appdeleate.coupons addObject:@"five"];
[appdeleate.coupons addObject:@"six"];
NSLog(@"%@",appdeleate.coupons);
}
It is work perfect for me, Hope it help you also.
精彩评论