Access NSArray from another class [duplicate]
Possible Duplicate:
IOS: call a method in another class
How can i pass an NSArray
object to another class ? I don't want to use extern
to access it, so is there any other way i could accomplish this ?
Also note, i am a beginner
In this example tableDataSource
is a NSArray
that can be accessed as a property of a class.
In your interface
declaration (iPadTableWithDetailsViewController.h
):
@interface iPadTableWithDetailsViewController : UIViewController {
NSArray *tableDataSource;
}
@property (nonatomic, retain) NSArray *tableDataSource;
Then, in your implementation
definition (iPadTableWithDetailsViewController.m
):
#import "iPadTableWithDetailsViewController.h"
@implementation iPadTableWithDetailsViewController
@synthesize tableDataSource;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
self.tableDataSource = nil;
}
return self;
}
- (void)viewDidLoad {
if (!tableDataSource) {
self.tableDataSource = [NSArray array];
}
}
.....
@end
And then you can access this from another class like this:
- (void)doSomething {
iPadTableWithDetailsViewController *myViewController = [[iPadTableWithDetailsViewController alloc] initWithNibName:@"iPadTableWithDetailsViewController" bundle:nil];
myViewController.tableDataSource = [NSArray arrayWithObjects:@"object1", @"object2", nil];
NSLog(@"myViewController.tableDataSource: %@", [myViewController.tableDataSource description];
}
More good info and examples:
Properties in Objective-C
Tutorial: Using Properties in Objective-C
cocoadevcentral learn objective-c
精彩评论