0

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

Community
  • 1
  • 1
Dexter
  • 53
  • 6

1 Answers1

2

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

chown
  • 51,908
  • 16
  • 134
  • 170
  • What does this method do `- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil ` ? – Dexter Oct 07 '11 at 17:51
  • 1
    It initializes a custom subclass of a `UIViewController`. I suggest you create a template project in Xcode and read through the comments and examples, there is a lot of good info there. – chown Oct 07 '11 at 17:53