【问题标题】:Inheritance on view controller Objective c视图控制器Objective c的继承
【发布时间】:2014-01-26 23:17:29
【问题描述】:

我正在尝试创建一个通用视图控制器和两个其他视图,它们都将继承自。

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

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

这些是继承上述文件的两个文件:

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

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

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

意味着永远不会调用createDeck 函数,

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

我尝试过的:

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

只有当我尝试启动第二个工作视图时才会调用这个,而不是坏的。

如果有什么不足的地方,请告诉我,我会补充的。

【问题讨论】:

    标签: ios objective-c inheritance uiviewcontroller cs193p


    【解决方案1】:

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

    我的猜测是,当您在 IB (Interface Builder) 中设置标签栏控制器时,您将其中一个标签设置为通用 CardGameViewController 的实例,而不是 SetGameViewController。

    【讨论】:

    • 也试过了...实际上我也将有问题的文件更改为 ׳PlayingCardGameViewController׳,但它仍然无法正常工作
    • 我是目标 c 的新手,是否可以将一个属性连接到 2 个或更多视图?即使该属性(连接到两个视图的任何一个)看起来都可以连接,但只有一个视图响应它。怎么可能?
    • 您一直交替使用术语视图和视图控制器。别。视图控制器不是视图。他们完全不同。您是否在询问是否可以将 IBOutlet 属性连接到多个视图?答案是否定的。
    • 这可能是正在发生的事情,代码看起来不错。在情节提要中,您可能没有引用正确的控制器。您可以在 CardGameViewController createDeck 中放置一个 NSLog 以查看它是否被调用,因为它不应该被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多