0

我是 iOS 编程和整个编程的新手,我目前正在从事的项目有问题。我正在开发一个测验应用程序,我想将我的问题存储在一个包含问题数组的对象中。我的测验 ViewController 然后导入问题库并UILabel在视图中显示这些问题。

抱歉,如果这有点像菜鸟问题,但我似乎无法弄清楚,网络搜索也没有太大帮助。我还处于学习过程的早期阶段。

我有: ViewController.h ViewController.m QuestionBank.h QuestionBank.m

在 QuestionBank.h 中:

//QuestionBank.h

#import <Foundation.h>

@interface QuestionBank : NSObject

- (id)initWithQuestionsArray:(NSMutableArray *)questions;
@property (nonatomic, copy) NSMutableArray *questions;
@property in currentQuestionIndex;
@property (nonatomic, copy) NSString *currentQuestion;
@end

在 QuestionBank.m 中:

//QuestionBank.m

#import "QuestionBank.h"

@implementation QuestionBank;
@synthesize questions _questions;
@synthesize currentQuestionIndex;
@synthesizeCurrentQuestion;

- (id)initWithQuestionsArray:(NSMutableArray *)questions
   {
    self = [super init];
    if (self) {
    questions = [[NSMutableArray alloc] init];
    [_quesitons addObject:@"What is the 14 +6?];
    [_questions addObject:@"What is my name?"];
    [_questions addObject:@"What is my age?"];
    }
    return self;
}

- (void)setCurrentQuestion:(NSString *)q
{
    q = [_questions objectAtIndex:currentQuestionIndex];
}

在 ViewController.h

//ViewController.h
#import <UIKit/UIKit>
@class QuestionBank;

@interface ViewController : UIViewController <UINavigationController Delegate>

@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@property (retain, nonatomic) QuestionBank *questionBank;

在 ViewController.m 中

#import "ViewController.h"
#import "QuestionBank.h"

@implementation ViewController
@synthesize questionLabel;
@synthesize questionBank;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = _subjectName;

    if (self.title == @"AGK") {
    [_questionLabel setText:self.questionBank.currentQuestion];
    NSLog(@"AGK was selected");
    }
}

使用代码,我在数组中遇到的问题没有出现在UILabel(标签正确连接到ViewController)中。

请随时指出任何错误,分享我如何让它工作的任何建议,以及我可以在代码的其他任何地方进行的任何一般性改进。

4

1 回答 1

0

您的代码有很多问题,需要进行一些修复。这里有几个:

我认为您并不完全理解在 Objective C 中如何将变量传递给函数。例如,在您的 - (void)setCurrentQuestion:(NSString *)q 方法中,您实际上并没有做任何事情。例如,请允许我在一个假装程序中演示以下代码:

- (void)testStringPassing:(NSString *)string
{
    string = @"Goodbye World!";
}

- (void)viewDidLoad
{
    NSString *string = @"Hello World.";
    [self testStringPassing:string];
    NSLog(@"%@", string);
}

在该示例代码中,您将记录“Hello World”,而不是“Goodbye World”。所以,本质上, -(void)testStringPassing:(NSString *)string 并没有真正做任何事情。如果你想改变“字符串”的值,你会想做更多这样的事情:

- (NSString *)testStringPassing
{
    return @"Goodbye World";
}

- (void)viewDidLoad
{
    NSString *string = @"Hello World";
    string = [self testStringPassing];
    NSLog(@"%@", string);
}

该代码将记录“再见世界”,因为您为 testStringPassing 返回一个值并将“字符串”设置为等于该值。

同样,这与您的代码有关,因为您的 -(void)setCurrentQuestion(NSString*)q 方法就像您没有真正完成任何事情的第一个示例。

您必须在使用它们之前分配和初始化您的类。在您的 ViewController 中,您尝试在分配和初始化之前使用您的 questionBank 类。尝试这样的事情:

- (void)viewDidLoad
{
    [super viewDidLoad]
    self.questionBank = [[QuestionBank alloc] init];

    //the rest of your code here
}

上述两个问题的组合是您使用 - (id)initWithQuestionsArray:(NSMutableArray *)questions 作为自定义初始化程序,但您没有将传递的值用于任何事情。在您当前的代码中,我建议您将 -(id)initWithQuestionsArray:(NSMutableArray *)questions 代码替换为以下代码:

- (id)init
{
    self.questions = [[NSMutableArray alloc] init];
    [self.questions addObject:@"Your Question Text Goes Here!!!!"];
    [self.questions addObject:@"Another question can go here."];
}

下划线命名约定在 Objective C 中有点不受欢迎,因为它违背了合成属性和使用生成的 getter 和 setter 的观点。特别是因为你刚刚学习,我会放弃下划线符号。代替

@synthesize questions _questions;

@synthesize questions;

然后,在您访问问题数组的任何地方,使用 self.questions 而不是 _questions。

无论如何,这应该足以让你开始/指出正确的方向。我强烈建议拿起一本只关注 Objective C 的书。学习 iOS 很棒,但是有这么多资源假设你已经知道或熟悉 Objective C,但似乎情况并非如此。

于 2012-08-30T18:44:42.190 回答