Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Passing data from array in data store object to UILabel in View

I am new to iOS Programming, and programming as a whole, and I have an issue with a project I am currently working on. I am working on a quiz application where I would like to store my questions in an object containing the array of questions. My quiz ViewController then imports the question bank and displays those questions in a UILabel in the View.

Sorry if this is somewhat of a noob issue but I can't seem to figure it out and web searches have not helped much either. I am still quite early in the learning process.

I have:
ViewController.h
ViewController.m
QuestionBank.h
QuestionBank.m

In QuestionBank.h:

//QuestionBank.h

#import

@interface QuestionBank : NSObject
{

}

- (id)initWithQuestionsArray:(NSMutableArray *)questions;

@property (nonatomic, copy) NSMutableArray *questions;

@property int currentQuestionIndex;

@property (nonatomic, copy) NSString* currentQuestion;

@end

In QuestionBank.m:

//QuestionBank.m

#import "QuestionBank.h"

@implementation QuestionBank

@synthesize questions = _questions;

@synthesize currentQuestionIndex;

@synthesize currentQuestion;

- (id)initWithQuestionsArray:(NSMutableArray *)questions
{
self = [super init];
if (self) {
questions = [[NSMutableArray alloc] init];

[_questions addObject:@"What is the capital 14 +6?"];

[_questions addObject:@"What is my name?"];

[_questions addObject:@"What is my age?"];
}
return self;
}

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


In ViewController.h

//ViewController.h

#import

@class QuestionBank;

@interface ViewController : UIViewController
{
}

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


In 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");
}

}

With this code the question I have in the array does not appear in the UILabel (The label properly hooked up to the ViewController).

Please feel free to point out any errors, share any suggestions for how I could get this to work, and any general improvements I can make anywhere else in my code.
Post edited by lybron on
Sign In or Register to comment.