別のクラスによって設定されるプロパティを設定しようとすると問題が発生します。テーブルにデータを入力するために使用される 2 つのプロパティを次に示します。
@interface TableViewController : UITableViewController
@property (nonatomic, retain) NSArray *friendsNames;
@property (nonatomic, retain) NSArray *friendsIDs;
@end
このクラス(から継承NSObject
)では、このメソッドを使用してそれらを設定しています:
@implementation MyFacebooDelegate
-(void)getFriends:(NSArray *)names {
NSMutableArray *friendsNames = [[NSMutableArray alloc] init];
NSMutableArray *friendsIDs = [[NSMutableArray alloc] init];
for (int i=0; i<[names count]; i++) {
NSDictionary *friend = [names objectAtIndex:i];
NSString *name = [friend objectForKey:@"name"];
[friendsIDs addObject:[friend objectForKey:@"id"]];
[friendsNames addObject:name];
}
if(tableController == nil)
tableController = [[TableViewController alloc] init];
[tableController setFriendsIDs:friendsIDs];
[tableController setFriendsNames:friendsNames];
NSLog(@"%@", [tableController friendsNames]);
[friendsNames release];
[friendsIDs release];
}
名前はすべてコンソール ( NSLog(@"%@", [tableController friendsNames])
) に表示されますが、TableViewController クラスには表示されません。
TableViewController クラスの 2 つの配列の内容を出力しようとするとviewDidLoad
、null になり、テーブルが画面に表示される前に配列も設定されます。