1

I have 10 buttons and i wrote the button action using switch case. When i click each button, it will increase some amount of values. If i am using 10 different variables for set the count value variable, it will work. But is any other possible way to achieve this?

I want to change the values dynamically in the mutable array.

For Eg:

    -(IBAction) btnAction : (id) sender{

       switch (btn.tag) {

                case 0:
               // amountArray is mutable array.

                    startCocoCount = startCocoCount + 10;

                    //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                //[amountArray addObject:str];

                break;

            case 1:

                startCocoCount = startCocoCount + 15;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];


                break;

            case 2:
                 startCocoCount = startCocoCount + 20;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            case 3:
                 startCocoCount = startCocoCount + 25;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];


                break;

            case 4:
                 startCocoCount = startCocoCount + 30;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];


                break;

            case 5:
                  startCocoCount = startCocoCount + 35;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            case 6:
                  startCocoCount = startCocoCount + 40;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            case 7:

                         startCocoCount = startCocoCount + 50;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            case 8:

                        startCocoCount = startCocoCount + 60;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            case 9:
                 startCocoCount = startCocoCount + 20;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            case 10:
                        startCocoCount = startCocoCount + 20;

                //NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];

                        //[amountArray addObject:str];

                break;

            default:
                break;
        }
}

In table view,

cell.textLabel.text = [amountArray objectAtindex:indexPath];

If i am using single variable, it won't set for all the conditions, so how can i achieve this?

2
  • @Devan What do you store in NSMutableArray ? I mean how many objects are there ? Which object you want to update ? Commented Jul 14, 2011 at 12:48
  • @Jennis, Actually i am having 10 buttons, so i want to store the 10 objects in the mutable array. I want to get the values which buttons user clicks. For eg: user clicks 3rd button, i will display 30. Thanks. Commented Jul 14, 2011 at 13:14

3 Answers 3

1

You can try this. It will work.

int startCocoCount;

NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for(int i=0;i<11;i++)
{
    [dataArray addObject:@"0"];
}

And for the method it will as follows:

-(IBAction) btnAction : (id) sender{

    switch ([sender tag]) {

        case 0:

            startCocoCount = startCocoCount + 10;

            [dataArray replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:0] intValue] + startCocoCount]];

            break;

        case 1:

            startCocoCount = startCocoCount + 15;

            [dataArray replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:1] intValue] + startCocoCount]];

            break;

        case 2:
            startCocoCount = startCocoCount + 20;

            [dataArray replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:2] intValue] + startCocoCount]];

            break;

        case 3:
            startCocoCount = startCocoCount + 25;

            [dataArray replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:3] intValue] + startCocoCount]];

            break;

        case 4:
            startCocoCount = startCocoCount + 30;

            [dataArray replaceObjectAtIndex:4 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:4] intValue] + startCocoCount]];

            break;

        case 5:
            startCocoCount = startCocoCount + 35;

            [dataArray replaceObjectAtIndex:5 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:5] intValue] + startCocoCount]];

            break;

        case 6:
            startCocoCount = startCocoCount + 40;

            [dataArray replaceObjectAtIndex:6 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:6] intValue] + startCocoCount]];

            break;

        case 7:

            startCocoCount = startCocoCount + 50;

            [dataArray replaceObjectAtIndex:7 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:7] intValue] + startCocoCount]];

            break;

        case 8:

            startCocoCount = startCocoCount + 60;

            [dataArray replaceObjectAtIndex:8 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:8] intValue] + startCocoCount]];

            break;

        case 9:
            startCocoCount = startCocoCount + 20;

            [dataArray replaceObjectAtIndex:9 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:9] intValue] + startCocoCount]];

            break;

        case 10:
            startCocoCount = startCocoCount + 20;

            [dataArray replaceObjectAtIndex:10 withObject:[NSString stringWithFormat:@"%d", [[dataArray objectAtIndex:10] intValue] + startCocoCount]];

            break;

        default:
            break;
    }
}

Let me know if you have any question.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer. Actually if i am using this code, the startCocoCount value should be retain which means, in first button i have clicked and get the value 90 and goes to the second button, now it would be increase 90 + 15. But i want to, when clicks the every button in the first time the value should be start with zero. That's why i am asking, how to use single variable to change the values properly. Thank you!
Here you can intialize startCocoCount to 0 each time button is pressed. I changed my answer.
If i set the startCocoCount = 0; means, every time it set as zero. when moves to one button to another. For eg: If i have first button value 90 and second button value 80, then again comes to the first button, then the value is zero. So It doesn't work.
Now you check again. I changed my answer.
What you have edited?. It was same as you have posted the first answer.
|
1

NSNumber allows you to store into an array. To access the array by index you will need to pre-populate the array with NSNumbers so that the indexes are available.

//You will need to know the index otherwise use a dictionary with named keys
NSNumber *num = [amountArray objectAtIndex:index];
num = [NSNumber numberWithInt:[num intValue] + 20];
[amountArray replaceObjectAtIndex:index withObject:num];

Comments

0

I think you can simplify like this

 -(IBAction) btnAction : (id) sender{

startCocoCount = startCocoCount + btn.tag;
}

You can set the tags of the button to hold the values.

4 Comments

Thanks for your answer. But how it's possible, in first button i have to add 10 and second button 15, third button 25. so each buttons having different values, so how can i do that? Thanks
@PugalDevan I think he means each button has its own tag
Yes thats rite. Each button has its own tag.
You can do it using interface builder or where ever you create those buttons. Write the tag value into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.