问题
I am trying to create a generic view controller and 2 other views which both will inherit from.
Now this is the generic view (what i think is relevant - if something else is needed i will add it):
@interface CardGameViewController ()
@property (strong, nonatomic) IBOutlet UITabBarItem *TabSelection;
@property (strong, nonatomic) CardMatchingGame *game;
@property (strong, nonatomic) IBOutlet UIButton *resetButton;
@property (weak, nonatomic) IBOutlet UILabel *scoreLable;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@end
@implementation CardGameViewController
- (CardMatchingGame*)game
{
if(!_game) _game = [[CardMatchingGame alloc]initWithCardCount:[self.cardButtons count] usingDeck:[self createDeck] gameMode:[self getActiveTabWithString:self.TabSelection.description]];
return _game;
}
- (Deck*)createDeck //abstract
{
return nil; <------- Relevant generic function
}
and these are the 2 files which inherit the mentioned file:
SetGameViewController.h:
#import "CardGameViewController.h"
@interface SetGameViewController : CardGameViewController
@end
SetGameViewController.m:
#import "SetGameViewController.h"
#import "SetCardDeck.h"
@interface SetGameViewController ()
@end
@implementation SetGameViewController
-(Deck*)createDeck
{
return [[SetCardDeck alloc]init];
}
@end
and the second one:
PlayingCardGameViewController.h:
#import "CardGameViewController.h"
@interface PlayingCardGameViewController : CardGameViewController
@end
PlayingCardGameViewController.m:
#import "PlayingCardGameViewController.h"
#import "PlayingCardDeck.h"
@interface PlayingCardGameViewController ()
@end
@implementation PlayingCardGameViewController
- (Deck*)createDeck
{
return [[PlayingCardDeck alloc]init];
}
@end
I also have tab bar navigator to switch between the 2 views.
The thing is, the first (SetGameViewController) is never instantiated no matter what i do,
Meaning the createDeck
function is never called,
While the second one (PlayingCardGameViewController) is ok every time.
What i have tried:
placing break points on the main view - (CardMatchingGame*)game
function.
this one only gets called when i am trying to start the second working view, and not the bad one.
If there is anything missing to give a certain lead, please let me know and i will add it.
回答1:
Set breakpoints on the empty createDeck method in the base class, as well as on the method in your two subclasses.
My guess is that when you set up your tab bar controller in IB (Interface Builder) , you made one of the tabs be an instance of the generic CardGameViewController instead of a SetGameViewController.
来源:https://stackoverflow.com/questions/21002714/inheritance-on-view-controller-objective-c