views:

104

answers:

4

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.

A: 

Firstly, you're probably looking for -[UIButton setTitle:forState:], where state is probably going to be UIControlStateNormal. The other thing you might want to use is -[NSString intValue].

This would let you do

total += [temp2 intValue];

rather than your case statement.

Ben Gottlieb
NSString *temp2 = [pnumbers objectAtIndex:i];
Emily Hughes
duh! sorry, meant where do I add in the intValue into the above line, or is it somewhere else? It's not something I've used before...
Emily Hughes
Have tried [b1 setTitle:@"b" forState:UIControlStateNormal]; but it isn't working - no idea what i've done to it!
Emily Hughes
Make sure b1 is hooked up; NSLog(...) it before calling. You can replace your entire switch(...) statement with the intValue line. Also, see Jehr's comment above about further condensing your code .
Ben Gottlieb
+1  A: 

It seems like your code could be simplified:

- (IBAction) pressed:(UIButton *)sender {
  int buttonIndex = [button tag];
  total += [[pnumbers objectAtIndex:buttonIndex] intValue];
}
Dave DeLong
Thank you, this worked beautifully! I knew my approach wasn't very elegant, but intValue is a new one for me, so I hadn't sussed the simpler route.
Emily Hughes
A: 

First, you should be able to simplify your current code a bit:

- (void)pressed:(id)sender
{
    UIButton *button = (UIButton *)sender;
    total += [sender tag];
}

Then you can call the button's -setSelected: method to change its state:

- (void)pressed:(id)sender
{
    UIButton *button = (UIButton *)sender;
    total += [sender tag];

    [button setSelected:YES];
}

When you create the buttons, use the following method to set its title:

- (void)setTitle:(NSString *)title forState:(UIControlState)state

If you use UIControlStateNormal as the argument, it will use the title for all three of the button's possible states.

jlehr
A: 

Many thanks to all who helped! I finally managed to achieve what I wanted using this beautifully simple bit of code:

-(IBAction) pressed:(id)sender {

UIButton *button = (UIButton *)sender;
if ([sender isSelected] ==FALSE) {  
[sender setSelected:TRUE];
int buttonIndex = [button tag];
total += [[pnumbers objectAtIndex:buttonIndex] intValue];
NSLog(@"%i", total);
}
else {
    [sender setSelected:FALSE];
    int buttonIndex = [button tag];
    total -= [[pnumbers objectAtIndex:buttonIndex] intValue];
    NSLog(@"%i", total);
}

}

Emily Hughes
decided it was easier to leave the values displayed in a UITextField, populated from the array, and access the array to get values.
Emily Hughes