将数据从数据存储中的可变数组传递到View中的UILabel

时间:2012-08-30 17:50:13

标签: iphone objective-c xcode ios5

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

很抱歉,如果这是一个noob问题,但我似乎无法弄明白,网络搜索也没有多大帮助。我还处于学习过程的早期阶段。

我有:     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)。

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

1 个答案:

答案 0 :(得分: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);
}

该代码将记录“Goodbye World”,因为您返回testStringPassing的值并将“string”设置为等于该值。

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

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

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

    //the rest of your code here
}

上述两个问题的组合是您使用 - (id)initWithQuestionsArray:(NSMutableArray *)问题作为自定义初始值设定项,但您没有使用传递的值进行任何操作。在您当前的代码中,我建议使用以下代码替换您的 - (id)initWithQuestionsArray:(NSMutableArray *)问题代码:

- (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。

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

相关问题