Как некоторые из вас, ребята, сказали, что это должно работать, и это так. У меня были некоторые ошибки в моем коде. Но хотел привести небольшой пример кода, чтобы доказать, что так и должно быть:
#import "PointersAppDelegate.h"
#import "PointersViewController.h"
#import "ClassA.h"
#import "ClassB.h"
@implementation PointersAppDelegate
@synthesize window=_window;
@synthesize viewController=_viewController;
@synthesize classA, classB;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
classA = [[ClassA alloc] init];
classB = [[ClassB alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
//Add new word to words in classA
[[classA words] addObject:@"two"];
//Print words in classB
[classB print];
return YES;
}
- (void)dealloc
{
[_window release];
[_viewController release];
[classA release];
[classB release];
[super dealloc];
}
@end
// Класс A
// ClassA.h
@interface ClassA : NSObject {
NSMutableArray *words;
}
@property(nonatomic,retain)NSMutableArray *words;
@end
// ClassA.m
#import "ClassA.h"
@implementation ClassA
@synthesize words;
- (id)init{
self = [super init];
if (self) {
words = [[NSMutableArray alloc] initWithObjects:@"one", nil];
}
return self;
}
- (void)dealloc {
[words release];
[super dealloc];
}
@end
ClassB
// ClassB.h
#import <Foundation/Foundation.h>
@interface ClassB : NSObject {
NSMutableArray *words;
}
@property(nonatomic,retain)NSMutableArray *words;
-(void)print;
@end
// ClassB.m
#import "ClassB.h"
#import "PointersAppDelegate.h"
@implementation ClassB
@synthesize words;
- (id)init{
self = [super init];
if (self) {
self.words = [[(PointersAppDelegate*)[[UIApplication sharedApplication] delegate] classA] words];
}
return self;
}
-(void)print{
for(int i=0;i<[words count];i++)
NSLog(@"%@", [words objectAtIndex:i]);
}
- (void)dealloc {
[words release];
[super dealloc];
}
@end
Результат:
2011-07-04 12:38:33.759 Pointers[20059:707] one
2011-07-04 12:38:33.767 Pointers[20059:707] two