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;
}
我做的,你前面提到的,直接訪問他們,但我是越來越空,因此它hought我必須明確地調用基類的版本,以獲得它,當然沒有運氣... – Genhain
此外,我看到的邏輯在你的答案,但是第一個建議是行不通的,因爲它會導致一個遞歸循環,所以它的意思是我會的必須重寫基類方法。因此這意味着不能傳入參數來以這種方式初始化子類? – Genhain
爲什麼會造成遞歸?你調用派生的構造函數來調用你的基構造器,並且你完成了。 – Jack