Tuesday, May 4, 2010

IPad Math: The code -- Math Problem subview

Math Problem

I create a small view that has the format of a math problem.   For some reason I couldn't reduce the size of the view from the size of an Ipad View in interface builder, however I put everything in the upper left corner and managed to reduce the size in the code:

This consists of several labels which the code will update.  I named them appropriately so that it was easier to monitor the connections.

Of course it looks a little silly in the full window:

And I have everything hooked up the class in interface builder, (along with a custom hidden button to detect clicks)
This shows the connections to the actual class.

The header contains the outlets and  a few actions, right now I have a forward delelaration of the controller class which allows me to update which item is currently selected.  I later intend to change it to a protocol which will allow more flexibility.

@class MathDrillViewController;

@interface MathProblem : UIViewController {
IBOutlet UILabel *first;
IBOutlet UILabel *second;
IBOutlet UILabel *result;
int correctResult;
int currentResult;
MathDrillViewController *controller;

@property (nonatomic,retain) UILabel *first;
@property (nonatomic,retain) UILabel *second;
@property (nonatomic,retain) UILabel *result;
@property (nonatomic) int correctResult;
@property (nonatomic) int currentResult;
@property (nonatomic,retain) MathDrillViewController *controller;
- (IBAction) setFocus:(id) sender;
- (void) setChoice:(int) choice;
- (void) clear;
- (id) initWithMax:(int) maxValue;
- (void) checkResult;
The main class contains the ability to change the labels as I randomly generate a subtraction problem.  Note that by adding an outlet for the operator, I could also change the operator if I so choose.

#import "MathProblem.h"

@implementation MathProblem
@synthesize currentResult;
@synthesize correctResult;
@synthesize first;
@synthesize result;
@synthesize second;
@synthesize controller;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    return self;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
int ifirst=arc4random()%10+1;
int isecond=arc4random()%ifirst+1;
first.text=[NSString stringWithFormat:@"%d",ifirst];
second.text=[NSString stringWithFormat:@"%d",isecond];
//return self;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

- (void)dealloc {
    [super dealloc];
- (IBAction) setFocus:(id) sender
if (self.controller.currentProblem!=nil)
self.controller.currentProblem.view.backgroundColor=[UIColor yellowColor];
self.view.backgroundColor=[UIColor blueColor];
- (void) setChoice:(int) choice
result.text=[NSString stringWithFormat:@"%d", choice];
if (choice==correctResult)
self.view.backgroundColor=[UIColor greenColor];
else {
self.view.backgroundColor=[UIColor redColor];

- (void) clear
- (id) initWithMax:(int) maxValue
return self;

- (void) checkResult


No comments:

Post a Comment