1

I have an NSMutableArray with multiple products. Each product has an amount. I want to update the amount of the associated product when a stepper is clicked. But all my products (whole NSMutableArray) are updated with the amount of the stepper.

    NSInteger index = stepper.tag;

    Product *p = [products objectAtIndex:index];
    p.amount = [NSNumber numberWithDouble:stepper.value];
   //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%d", stepper.tag] message:@"test" delegate:nil cancelButtonTitle:@"ok"otherButtonTitles:nil];
   //    [alert show];

   for (Product *p in products) {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@", p.amount] message:p.name delegate:nil cancelButtonTitle:@"ok"otherButtonTitles:nil];
       [alert show];
   }

Does anyone have a solution?

1 Answer 1

1

Since Product *p is a pointer, you don't have to remove and re-insert in the array. You can just modify the Product in place. Try this:

NSInteger index = stepper.tag;

Product *p = [products objectAtIndex:index];
p.amount = [NSNumber numberWithDouble:stepper.value];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but the result is still the same :s
I edited my question with whole code triggered when stepper is clicked.. (when I uncomment the one part, I see the right stepper.tag)
Are you sure that whole array was updated. Debug your code with an alertView is not the better way, because the show method was no stop on each element but stack it. Replace the alertView by NSLog and shown the result.

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.