2011-10-07 10 views
0

可能性の重複:私は別のクラスにNSArrayオブジェクトを渡すことができますどのように
IOS: call a method in another classのアクセスにNSArray

?私はそれにアクセスするためにexternを使用したくないので、これを達成できる他の方法はありますか?

も、私はtableDataSourceは、クラスのプロパティとしてアクセスすることができNSArrayである。この例では、初心者

答えて

2

です、注意してください。あなたimplementation定義における

@interface iPadTableWithDetailsViewController : UIViewController { 
    NSArray *tableDataSource; 
} 

@property (nonatomic, retain) NSArray *tableDataSource; 

次に、(iPadTableWithDetailsViewController.m):

あなた interface宣言で

iPadTableWithDetailsViewController.h

#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 

そして、あなたは、このような別のクラスからこれをアクセスすることができます。

- (void)doSomething { 
    iPadTableWithDetailsViewController *myViewController = [[iPadTableWithDetailsViewController alloc] initWithNibName:@"iPadTableWithDetailsViewController" bundle:nil]; 
    myViewController.tableDataSource = [NSArray arrayWithObjects:@"object1", @"object2", nil]; 
    NSLog(@"myViewController.tableDataSource: %@", [myViewController.tableDataSource description]; 
} 

その他のg OOD情報と例:

Properties in Objective-C
Tutorial: Using Properties in Objective-C
cocoadevcentral learn objective-c

+0

このメソッドは '何をする - (ID)initWithNibName:(NSStringの*)nibNameOrNilバンドル:(NSBundle *)nibBundleOrNil'? – Dexter

+1

'UIViewController'のカスタムサブクラスを初期化します。私はあなたがXcodeでテンプレートプロジェクトを作成し、コメントや例を読むことをお勧めします。たくさんの良い情報があります。 – chown