Inheritance on view controller Objective c

馋奶兔 提交于 2019-12-08 10:42:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!