Tuesday, May 4, 2010

IPad Math: The Code -- The controller.

The controller will go through and calculate where the views for the math problem should exist.  It then uses the extent of the view to calculate where the various items should go.  Note that it currently does not properly support rotation.  In addition I originally had a placeholder view that would contain the buttons, but for some reason I wasn't getting events, even though I turned on UI interaction for that view.


- (void)viewDidLoad {
    [super viewDidLoad];
int width=self.view.frame.size.width/6;
int height=(self.view.frame.size.height-100)/5;

for (int row=0;row<5;row++)
{
for (int column=0;column<6;column++)
{
MathProblem *problem=[[MathProblem alloc] initWithMax:10];
problem.view.frame=CGRectMake(column*width, height*row, width, height);
problem.controller=self;
// problem.view.frame=CGRectMake(40, 40, 100, 100);
[self.view addSubview:problem.view];
}
}

int buttonWidth=resultChoices.frame.size.width/10;
int buttonHeight=resultChoices.frame.size.height;
resultChoices.userInteractionEnabled=true;
self.view.userInteractionEnabled=true;
 
for (int i=0;i<10;i++)
{
UIButton *button=[[[UIButton alloc]init] retain];
button.tag=i;
button.frame=CGRectMake(i*buttonWidth,buttonHeight+resultChoices.frame.origin.y,buttonWidth,buttonHeight);
button.backgroundColor=[UIColor redColor];
button.userInteractionEnabled=true;
[button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonChosen:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}

- (IBAction) buttonChosen:(id)sender
{
UIButton *button=(UIButton*) sender;
int choice=button.tag;
if (currentProblem!=nil)
{
[currentProblem setChoice:choice];
}
}

No comments:

Post a Comment