【发布时间】:2014-03-08 07:23:10
【问题描述】:
我正在制作一个非常简单的应用程序,其中包含许多不同的测验。我的动机主要是学习 Objective-C 和 iOS 开发。所以我的应用中有一些视图,它们都具有我以编程方式设置的相同背景。我决定创建一个UIViewController 的子类,它添加了函数setBackground,并让我的应用程序中的所有ViewControllers 都继承这个子类(SumsViewController)。
这很好用,但现在我想取出每个测验共有的一些内容,并将其添加到 SumsViewController 的子类中,称为 QuizController。尝试过这个后,我收到关于 property x not found on object of type y 的错误。
代码如下:
SumsViewController.h
#import <UIKit/UIKit.h>
@interface SumsViewController : UIViewController
- (void)setBackground;
@end
SumsViewController.m
#import "SumsViewController.h"
@interface SumsViewController ()
@end
@implementation SumsViewController
- (void)setBackground
{
... //Code to set Background
}
@end
QuizController.h
#import <UIKit/UIKit.h>
#import "SumsViewController.h"
@interface QuizController : SumsViewController
@property (nonatomic) NSInteger number1;
@property (nonatomic) NSInteger number2;
@property (nonatomic) NSInteger difficulty;
@property (nonatomic) NSInteger score_val;
@property (nonatomic) NSTimer* countdown;
@property (nonatomic) NSInteger time_val;
@end
QuizController.m
#import "QuizController.h"
@interface QuizController ()
@end
@implementation QuizController
@end
现在这是一个继承 QuizController 的测验控制器:
AdditionController.h
#import <UIKit/UIKit.h>
#import "QuizController.h"
@interface AdditionController : QuizController
@end
AdditionController.m
#import "AdditionController.h"
#import "ViewController.h"
@interface AdditionController ()
@property (weak, nonatomic) IBOutlet UILabel *question;
@property (weak, nonatomic) IBOutlet UILabel *timer;
@property (weak, nonatomic) IBOutlet UIButton *ans1;
@property (weak, nonatomic) IBOutlet UIButton *ans2;
@property (weak, nonatomic) IBOutlet UIButton *ans3;
@property (weak, nonatomic) IBOutlet UILabel *score;
@end
@implementation AdditionController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setBackground]; //function declared in SumsViewController WORKS FINE
self.difficulty = 20; //declared in QuizController DOES NOT WORK
// Error says property not member of object AdditionController
[self setupGame];
}
@end
感谢您对此的任何帮助,我确信这是一个简单的误解,但这让我感到沮丧。
【问题讨论】:
-
我还注意到您的
NSTimer没有定义为strong,这几乎肯定是一个错误。应该是@property (nonatomic, strong) NSTimer* countdown; -
是的,当我复制它时是一个错字,它在我的代码中定义为
strong。
标签: ios objective-c inheritance