Skip to content

Instantly share code, notes, and snippets.

@zackshapiro
Created May 27, 2014 16:08
Show Gist options
  • Save zackshapiro/ead4b0d546b4c5cdfc3c to your computer and use it in GitHub Desktop.
Save zackshapiro/ead4b0d546b4c5cdfc3c to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface QuizViewController : UIViewController
{
int currentQuestionIndex;
// the model objects, pointers here. Referenced in QuizViewController.m
NSMutableArray *questions;
NSMutableArray *answers;
// the view objects - don't worry about IBOutlet, we'll get there shortly
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
// IBOutlet UILabel *thingField;
// IBOutlet UILabel *fooField;
// IBOutlet UILabel *NBSField;
// These show up in the Outlets section when right clicking on the File Owner
}
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
// These show up in the Received Actions section when clicking on File Owner
// IBOutlet and IBAction let me connect the controller and view objects in the Storyboard/XIB file
@end
#import "QuizViewController.h"
@interface QuizViewController ()
@end
@implementation QuizViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
// call the init method implemented by the superclass
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// create two arrays and make the pointers point to them
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
// add quetions and answers to the arrays
[questions addObject:@"What is 7 + 7?"];
[answers addObject:@"14"];
[questions addObject:@"What is the capitol of Vermont?"];
[answers addObject:@"Montpelier"];
[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];
}
// return the address of the new object
return self;
}
- (IBAction)showQuestion:(id)sender
{
// Step to the next question
currentQuestionIndex++;
// Am I past the last question?
if (currentQuestionIndex == [questions count]) {
// Go back to the first question
currentQuestionIndex = 0;
}
// Get the string at that index in the questions array
NSString *question = [questions objectAtIndex:currentQuestionIndex];
// Log the string to the console
NSLog(@"displaying question: %@", question);
// Display the string in the question field
[questionField setText:question];
// Clear the answer field
[answerField setText:@"???"];
}
- (IBAction)showAnswer:(id)sender
{
// What is the answer to the current question?
NSString *answer = [answers objectAtIndex:currentQuestionIndex];
// Display it in the answer field
[answerField setText:answer];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment