0

I am designing a make own pizza app. The user would add multiple toppings for example shrimp, bacon, and etc. on top of each other. So the adding image is working fine. Here is my code:

bool meatAdded;
- (void)addMeatTopping:(NSString*)meat  withImage:(UIImageView*)meatImage {
    if ((meatAdded =! meatAdded)) {
        //crust o taqir bede be cheese BADAN
        meatImage = [[UIImageView alloc]initWithFrame:_crustImage.bounds];
        [meatImage setImage:[UIImage imageNamed:meat]];
        [_crustImage addSubview:meatImage];

        for (UIButton*button in _meatButtonsArray) {
            checkMark.center = button.center;
            checkMark.alpha = 1;
        }

        [UIView animateWithDuration:.60 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.20 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
            meatImage.transform = CGAffineTransformMakeScale(.5, .5);
            meatImage.transform = CGAffineTransformMakeScale(1, 1);             } completion:nil];
    }else {
        /**** THIS PART OF CODE DOESN'T WORK ! IT PRINTS LOG BUT DOESN'T CHANGE THE MEAT IMAGE AT ALL ****/

        NSLog(@"Remove");
        meatImage.alpha = 0;
        [meatImage setImage:nil];
        [meatImage removeFromSuperview];
    }
}

example of calling above method:

- (IBAction)meat0:(id)sender {
    [self addMeatTopping:@"topping_Bacon" withImage:beykenMeat];
}

- (IBAction)meat1:(id)sender {
   [self addMeatTopping:@"Steak" withImage:steykMeat];
}

How can I create a toggle method when user select an item? The item will add to view and when taps again it should be removed from view.

4
  • why dont you load the previous image again, instead of undoing ? Commented Feb 28, 2016 at 15:57
  • 1
    What are you trying to do with this if statement? if ((meatAdded =! meatAdded)). I doubt that is what you actually want since that negates meatAdded and then assigns the new value to meatAdded and then finally checks to see if the new value is true or false. Commented Feb 28, 2016 at 16:00
  • @TejaNandamuri : there is no previous image. It adds by user step by step First crust - sauce - cheese (there are single choice) then meat , vegetables (these are multiple choice) Commented Feb 28, 2016 at 16:04
  • Change the log to NSLog(@"Remove:%@", meatImage)-- did meatImage somehow become nil somewhere else? Commented Feb 28, 2016 at 16:55

1 Answer 1

1
SubclassImageView *imageviewG = [[SubclassImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
self.crustImage = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 250, 250)];
self.crustImage.backgroundColor = [UIColor redColor];
[imageviewG setImage:@"topping_Bacon"];

[self.crustImage addSubview:imageviewG];
[self.view addSubview:self.crustImage];

    for(id viewInner in self.crustImage.subviews){
    if([viewInner isKindOfClass:[SubclassImageView class]]){
        SubclassImageView *imageSeleted = (SubclassImageView *)viewInner;
        if([imageSeleted.getImageName isEqualToString:@"topping_Bacon"]){
        //your logic to add or remove the imageview from super view
        }else if([imageSeleted.getImageName isEqualToString:@"Steak"]){
        //your logic to add or remove the imageview from super view
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

//SubclassImageView is subclass os UIimageView -(void) setImage:(NSString*)fileName { imageFileName = fileName; [super setImage:[UIImage imageNamed:fileName]]; } -(NSString*) getImageName{ return imageFileName; }

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.