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];
}
EDIT***
Ok so This is what I tried:
arrayTestViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface arrayTestViewController : UIViewController {
NSMutableArray *myArray;
@private NSMutableArray *myArray2;
}
-(IBAction)showArray;
-(IBAction)switchViews;
-(IBAction)addToArray;
@end
arrayTestViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
myArray2 = [[NSMutableArray alloc] init];
SecondViewController.myArray2 = myArray2;
//ERRORS:Accessing unknown 'setMyArray2:" class method.
//object cannot be set - either readonly property or no setter found
}
-(IBAction)switchViews{
SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
screen.myArray2 = self.myArray2;
//ERROR:Accessing unknown 'myArray2' getter method.
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
[screen release];
}
SecondViewController.h
#import <UIKit/UIKit.h>
#import "arrayTestViewController.h"
@interface SecondViewController : UIViewController{
NSMutableArray *myArray2;
}
@property (nonatomic, retain) NSMutableArray *myArray2;
-(IBAction)addToArray;
-(IBAction)switchBack;
@end
SecondViewController.m
#import "SecondViewController.h"
#import "arrayTestViewController.h"
@implementation SecondViewController
@synthesize myArray2;
-(IBAction)addToArray{
arrayTestViewController *object = [[arrayTestViewController alloc] init];
[object addToArray2];
}
It's giving me three errors (also commented in the code):
Accessing unknown 'setMyArray2:" class method. object cannot be set - either readonly property or no setter found. Accessing unknown 'myArray2' getter method.