2013-01-25 33 views
0

我有有一個自定義init方法,然後通過使用init方法傳入的值,然後調用它的子類定製的初始化方法的基類。問題是當我試圖訪問有分配給它們通過子類的基類通過super值的變量,該值是零,它像基類是完全不同的對象。是否因爲基類尚未從其init方法返回?還是我在這裏繼承了錯誤的方式?代碼要遵循。OBJ-C繼承變量是空

接口

@interface WTFGameBoard : NSObject 
{ 
    @protected 
    UIView *_answerView; 
    UIView *_keyboardView; 
    NSMutableArray* _answerSeperatedByCharacter; 

    WTFAnswerBoard *_answerBoard; 
    WTFGameKeyboard *_gameKeyboard; 

    OpenGameViewController *_weakGameViewRef; 
    GameInfo *_gameinfo; 
} 

-(id) initWithGameVC:(OpenGameViewController*)gameVC; 

@property (nonatomic,unsafe_unretained)OpenGameViewController *weakGameViewRef; 
@property (nonatomic,strong)GameInfo *gameInfo; 

@end 

實施

@implementation WTFGameBoard 
@synthesize weakGameViewRef = _weakGameViewRef; 
@synthesize gameInfo = _gameinfo; 

-(id) initWithGameVC:(OpenGameViewController*)gameVC 
{ 
    if (self = [super init]) 
    { 
     //[weakGameViewRef ] 
     _answerView = [gameVC answerView]; 
     _keyboardView = [gameVC keyboardView]; 

     self.weakGameViewRef = gameVC; 
     self.gameInfo = [[CurrentGamesInfo sharedCurrentGamesInfo]_selectedGame]; 

     _answerBoard = [[WTFAnswerBoard alloc] initWithAnswer:[gameVC answer] blankSpaceImageView:[gameVC answerBox]]; 
     _gameKeyboard = [[WTFGameKeyboard alloc] initWithButtons:[gameVC letterSelectButtons]]; 

    } 

    return self; 
} 

@end 

接口

@interface WTFAnswerBoard : WTFGameBoard 
{ 
    NSMutableArray *WTFAnswerSpaces; 
    NSMutableArray *_answerBlankBlocks; 
    NSMutableArray *_answerGiven; 
    NSMutableArray *_answerBlankOriginalPosition; 
    NSString *_answer; 
} 

-(id)initWithAnswer:(NSString*)answer blankSpaceImageView:(UIImageView*)answerBox; 

實施

-(id)initWithAnswer:(NSString*)answer blankSpaceImageView:(UIImageView*)answerBox 
{ 
    if (self = [super init]) 
    { 
     _weakGameViewRef = [super weakGameViewRef];//WHY U NO NOT BE NULL? 
     _gameinfo = [super gameInfo];//WHY U NO NOT BE NULL? 

     _answerBlankBlocks = [_weakGameViewRef answerBlankBlocks]; 
     _answerGiven = [_weakGameViewRef answerGiven]; 
     _answerBlankOriginalPosition = [_weakGameViewRef answerBlankOriginalPosition]; 

     [self SetupBlankAnswerSpacesForAnswer:answer withTemplate:answerBox]; 
    } 

    return self; 
} 

回答

0

問題是,你是不是調用在派生類中的自定義構造函數:

if (self = [super init]) 

要調用默認的,這是不覆蓋,而沒有按」 t初始化您嘗試訪問的ivars。

你應該打電話給你的自定義構造函數:

if (self = [super initWithGameVC:gameVC]) 

當然,這意味着你需要沿着參數傳遞,或者通過初始化你想,而不需要任何參數進行初始化什麼覆蓋默認的構造函數。

,我不understant另一件事就是爲什麼你在你的自定義類設置的派生類的實例變量:

_weakGameViewRef = [super weakGameViewRef]; 

這基本上什麼也不做,因爲伊娃是一樣的,如果你設置了一個你的基類可以直接訪問它。

編輯

既然你這裏有一個奇怪的依賴問題的快速解決辦法是有一些像

WTFAnswerBoard initWithWTFGameBoard:(WTFGameBoard*)board { 
    self.board = board; 
} 

這樣您就可以訪問該實例化WTFAnswerBoard板,並保持繼承,但切換用法組成(讓你的遞歸初始化不會發生添加屬性WTFAnswerBoard

+0

我做的,你前面提到的,直接訪問他們,但我是越來越空,因此它hought我必須明確地調用基類的版本,以獲得它,當然沒有運氣... – Genhain

+0

此外,我看到的邏輯在你的答案,但是第一個建議是行不通的,因爲它會導致一個遞歸循環,所以它的意思是我會的必須重寫基類方法。因此這意味着不能傳入參數來以這種方式初始化子類? – Genhain

+0

爲什麼會造成遞歸?你調用派生的構造函數來調用你的基構造器,並且你完成了。 – Jack