我有一個自定義類BoardMatchData,其中包含關於國際象棋比賽的信息。爲什麼我的實例變量零,即使我設置它?
我還得到了一個名爲BoardViewController的自定義UIViewController,當用戶從列表中選擇一個匹配項時就會分配這個UIViewController。
下面是從我創建BoardViewController片斷,設置它的委託,然後設置新BoardMatchData:
Games *selectedGame = [[self fetchedResultsController] objectAtIndexPath:indexPath];
if (!self.bvc) {
NSLog(@"Alloc-ing a BVC");
self.bvc = [[BoardViewController alloc] init];
self.bvc.delegate = self;
}
[self.bvc setNewBoardMatchData:[MasterViewController boardMatchDataFromGame:selectedGame]];
調試時,我可以看到這個方法setNewBoardMatchData被調用,它有有效的數據來進去。但是,稍後在BoardViewController中,此boardMatchData總是似乎爲零。這裏的setNewBoardMatchData方法:
- (void)setNewBoardMatchData:(BoardMatchData *)newBoardMatchData {
NSLog(@"BMD is being set");
if (self.boardMatchData != newBoardMatchData) {
self.boardMatchData = newBoardMatchData;
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
並在BoardViewController.h,我只是有一個實例變量:
BoardMatchData *boardMatchData;
方法聲明:
- (void)setNewBoardMatchData:(BoardMatchData *)newBoardMatchData;
然後在BoardMatchData頂部.m,我有:
@interface BoardViewController()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
@property (nonatomic,retain) BoardMatchData *boardMatchData;
- (void)configureView;
@end
@synthesize boardMatchData = _boardMatchData;
......我這裏的目的是要確保setter只是被自己調用,而不是被其他對象調用。
我想如何設置self.boardMatchData有什麼問題嗎?
我沒有做任何保留/發佈,因爲我使用ARC。
編輯:
迦勒 - 我這樣做,@synthesize聲明作爲我擺擺手周圍的部分試圖找到這個bug。原來我有:
@synthesize boardMatchData;
......我剛剛切換回來。行爲是一樣的; self.boardMatchData總是結束爲零,即使在我設置它之後。
我想現在我只有一個伊娃,boardMatchData,我總是通過self.boardMatchData訪問它。
我該如何防止它成爲零?