Can't add to NSMutableArray from SecondaryView
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.
I initially had tried this in the SecondViewController as well but the same outcome.
Thank you for your suggestion on not keeping them in the delegate :)
How would I fix this, given that I change it so that I initialize the Array in the the RootViewController, and I call the function from the SecondViewController?
-(IBAction)addToArray{
arrayTestAppDelegate *object = [[arrayTestAppDelegate alloc] init];
[object addToArray3];
}
The above code is wrong. You should not be alloc
'ing an app delegate inside a view controller. Like, never. Ever. (Also, you leak memory each time, since you didn't pair your alloc
/init
with a release
. But I digress...)
If you want to access your application delegate from your view controllers, you do it like so:
arrayTestAppDelegate* delegate = (arrayTestAppDelegate*)[[UIApplication sharedApplication] delegate];
[delegate addToArray];
While that answers your question, I strongly urge you to not use your application delegate as a holder for global variables. While it's not "bad" to create top-level arrays and such in your app delegate, you should be passing those objects down the view controller hierarchy as you create each view controller. This is cleaner in the sense that you do not have actions taking place at a distance. (Down with Singletons!)
(P.S. In Cocoa, the convention is that class names begin with uppercase letters -- arrayTestAppDelegate
should be renamed ArrayTestAppDelegate
)
Edit:
How would I fix this, given that I change it so that I initialize the Array in the the RootViewController, and I call the function from the SecondViewController?
First, the object that owns an object should probably be the one responsible for initializing the object. Don't put your array in your app delegate and then initialize it elsewhere. This is another form of "action at a distance" and is hard to read and debug later.
Second, to answer your question really requires more information about how your "RootViewController
" and "SecondViewController
" exist in your controller hierarchy. But I can give you a general answer about passing objects (like your array) along the line.
(I'm assuming that your RootViewController
is the top-level view controller in your app, and that SecondViewController
is a view controller shown by RootViewController
. Judging by the names, it's a safe bet.)
ArrayTestAppDelegate.h (note the capitalization) might look something like this:
...
@interface ArrayTestAppDelegate : NSObject <UIApplicationDelegate>
{
...
@private
NSMutableArray* theArray;
...
}
...
Note the lack of @property
.
ArrayTestAppDelegate.m might look something like this:
...
@implementation ArrayTestAppDelegate
...
- (void)applicationDidFinishLaunching:(UIApplication*)application
{
...
theArray = [[NSMutableArray alloc] init];
...
rootViewController.theArray = theArray;
[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
}
...
- (void)dealloc
{
[theArray release];
}
...
@end
RootViewController.h:
...
@interface RootViewController : UITabBarController
{
...
NSMutableArray* theArray;
...
}
...
@property (nonatomic, retain) NSMutableArray* theArray;
...
RootViewController.m
@implementation RootViewController
...
@synthesize theArray;
...
- (void)dealloc
{
[theArray release];
}
...
- (void)showSecondViewController
{
SecondViewController* secondViewController = [[SecondViewController alloc] initWithNib:@"SecondView"];
secondViewController.theArray = self.theArray;
// Show secondViewController here -- modally, using nav controller stack, etc.
[secondViewController release];
}
...
@end
And so on. I leave to the reader the exercise of implementing theArray
@property
for SecondViewController
(hint: much like RootViewController
)
精彩评论