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;
@endIn 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.