繁体   English   中英

在视图控制器目标c上继承

[英]Inheritance on view controller Objective c

我试图创建一个通用的视图控制器和其他两个都将继承的视图。

现在这是通用视图(我认为是相关的-如果需要其他内容,我将其添加):

@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
}

这是继承上述文件的2个文件:

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

第二个:

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

我还具有标签栏导航器,可以在2个视图之间切换。

事实是,无论我做什么,第一个(SetGameViewController)都不会实例化,

表示从不调用createDeck函数,

每次第二个(PlayingCardGameViewController)都可以。

我试过的

在主视图上放置断点- (CardMatchingGame*)game功能。

仅当我尝试启动第二个工作视图时才调用此视图,而不是坏视图。

如果有任何不足以提供一定的领先优势,请让我知道,我将添加它。

在基类中的空createDeck方法以及您的两个子类中的方法上设置断点。

我的猜测是,当您在IB(界面生成器)中设置选项卡栏控制器时,使其中一个选项卡成为通用CardGameViewController的实例而不是SetGameViewController的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM