2011-10-07 105 views

回答

2

在這個例子中tableDataSource是可以作爲一個類的屬性來訪問的NSArray

在你interface聲明(iPadTableWithDetailsViewController.h):

@interface iPadTableWithDetailsViewController : UIViewController { 
    NSArray *tableDataSource; 
} 

@property (nonatomic, retain) NSArray *tableDataSource; 

然後,在你implementation定義(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 

然後你就可以從這樣的其他類訪問此:

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

更多g洪水信息和示例:

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