开发者

Access NSArray from another class [duplicate]

Thi开发者_Go百科s question already has answers here: Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜