Hi, I want to create a basic app where you have 8 numbered buttons, with a randomly generated number on each. You are given a total value, and have to click on buttons whose numbers add up to that value.
I need to show which buttons have been 'used' already (I hope to use the selected state for that, as I am using a custom button).
When you press a button, it needs to add the correct value to the 'totalSoFar and change the button to it's selected state.
The plan is that I currently have 8 UIButtons (b1, b2 etc). I have generated a random array of numbers, that I want to assign as the title of the button.
I have tried about a million variations on the theme of [b1 setTitle etc] but can't get it to work.
I have added a textview on top of the button (n1, n2...) that now displays my random numbers instead.
I have got it so that when you press a button, it gets the tag of that button, and then finds the matching position in the array, so I can use that value in the 'what's the total of all the buttons I have pressed' calculation. I cannot however figure out how to access the sender button in order to tell it to change to selected.
I'd much rather do this by just changing the title of the button, rather than having to have an extra textfield.
I am still quite new to Objective C, having previously only coded in actionscript for Flash, and I'm only asking after spending about 4 hours trying to search for a solution online (and swearing at my computer a lot...)
Here's what I have so far... I have tagged each button, so that b1 has a tag of 0, b2 has a tag of 1 etc...
-(IBAction) pressed:(id)sender {
int i;
UIButton *button = (UIButton *)sender;
for (i=0; i<8; i++) {
if ([button tag] == i)
{
//returns value in array as a string
NSString *temp2 = [pnumbers objectAtIndex:i];
//checks string value to see what total to add
if ([temp2 isEqualToString:@"1"]) {
total +=1;
}
if ([temp2 isEqualToString:@"2"]) {
total +=2;
}
if ([temp2 isEqualToString:@"3"]) {
total +=3;
}
if ([temp2 isEqualToString:@"4"]) {
total +=4;
}
if ([temp2 isEqualToString:@"5"]) {
total +=5;
}
if ([temp2 isEqualToString:@"6"]) {
total +=6;
}
if ([temp2 isEqualToString:@"7"]) {
total +=7;
}
if ([temp2 isEqualToString:@"8"]) {
total +=8;
}
if ([temp2 isEqualToString:@"9"]) {
total +=9;
}
}
}
}
I don't want to just make a new function for each separate button (as I plan to have 64 buttons in the end) in order to be able to access which button it was without lots of if and for statements. If nothing else, that seems like bad coding to me! Hope this all makes sense... I mostly just want to make the damn thing show the selectedState for the correct button once I've clicked on it. I can live with the textfields!
EDIT - for the record, I've now managed to get it to the selected state, by using [b1 setSelected:FALSE];
at the start, and then adding
UIButton *button = (UIButton *)sender; [sender setSelected:TRUE];
before the for loop. Still not sure how to get it to deselect the next time it is clicked though.