iPhone: Using a NSMutableArry in the AppDelegate as a Global Variable
        Posted  
        
            by aahrens
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by aahrens
        
        
        
        Published on 2010-04-16T17:38:27Z
        Indexed on 
            2010/04/16
            18:13 UTC
        
        
        Read the original article
        Hit count: 385
        
What i'm trying to accomplish is to have an NSMutableArray defined in the AppDelegate. I then have two UIViewControllers. One view is responsible for displaying the array from the AppDelegate. The other view is used to add items to the array. So the array starts out to be empty. View1 doesn't display anything because the array is empty. The User goes to View2 and adds an item to the array in AppDelegate. Then when the user goes back to View1 it now displays one item.
Here is how I'm trying to accomplish this
@interface CalcAppDelegate : NSObject <UIApplicationDelegate> {
 UIWindow *window;
 UITabBarController *tabBarController;
 NSMutableArray *globalClasses;
}
@property (nonatomic,retain) NSMutableArray *globalClasses;
My other view
In the viewDidload I set the array in my View to be the one in the AppDelegate. In an effort to retain values.
allCourses = [[NSMutableArray alloc]init];
CalcAppDelegate *appDelegate = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
allCourses = appDelegate.globalClasses;
Then I would update my allCourses array by adding a new item. Then try to set the array in the AppDelegate to be equal to the modified one.
CalcAppDelegate *appDel = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"Size before reset %d",[appDel.globalClasses count]);
    appDel.globalClasses = allCourses;
    NSLog(@"Size after reset %d",[appDel.globalClasses count]);
What I'm seeing that's returned is 2 in the before, and 2 after. So it doesn't appear to be getting updated properly. Any suggestions?
© Stack Overflow or respective owner