Can't add to NSMutableArray from SecondaryView
Posted
by Antonio
on Stack Overflow
See other posts from Stack Overflow
or by Antonio
Published on 2010-06-12T23:09:07Z
Indexed on
2010/06/12
23:12 UTC
Read the original article
Hit count: 257
Hi guys, I've searched and read and still haven't found a concrete answer.
Brief: I have an application where I declare an NSMutableArray in my AppDelegate to synthesize when the application loads. (code below). I have a SecondaryViewController call this function, and I have it output a string to let me know what the array size is. Every time I run it, it executes but it does not add any objects to the array. How do I fix this?
AppDelegate.h file
#import <UIKit/UIKit.h>
@class arrayTestViewController;
@interface arrayTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
arrayTestViewController *viewController;
NSMutableArray *myArray3;
}
@property (nonatomic, retain) NSMutableArray *myArray3;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet arrayTestViewController *viewController;
-(void)addToArray3;
@end
AppDelegate.m file
#import "arrayTestAppDelegate.h"
#import "arrayTestViewController.h"
@implementation arrayTestAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize myArray3;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myArray3 = [[NSMutableArray alloc] init];
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
-(void)addToArray3{
NSLog(@"Array Count: %d", [myArray3 count]);
[myArray3 addObject:@"Test"];
NSLog(@"Array triggered from SecondViewController");
NSLog(@"Array Count: %d", [myArray3 count]);
}
SecondViewController.m file
#import "SecondViewController.h"
#import "arrayTestAppDelegate.h"
@implementation SecondViewController
-(IBAction)addToArray{
arrayTestAppDelegate *object = [[arrayTestAppDelegate alloc] init];
[object addToArray3];
}
© Stack Overflow or respective owner