Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

does any one know how ...

does any one know how to send an if statement for a uitapgesture. like when you are on instagram and a photo is already tapped it would do another method and unlike the photo

10 Answers

well one way would be to add a BOOL property that holds whether or not that objects been liked and basically toggle that BOOL on the tap.

so basically in viewWillAppear you would need to set your property to NO intially

self.liked = NO;

then in your method that runs when you tap the item

    if (self.liked == NO) {

      //item has not been liked yet, so you want to like it
      self.liked = YES;
      //save the like to the backend or whatever

    } else {

       //already been liked, now you want to unlike it
       self.liked = NO;
      //save the unlike to the backend or whatever

    }

what i did was create a method to like a photo on instagram and then i made another method to unlike a photo on instagram now i see what your saying but how would i connect the if statement with the uitapgesture

well you would use a selector when you initialize your tap gesture

self.liked = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTapped)];
tap.numberOfTapsRequired = 2;
[self addGestureRecognizer:tap];

then in your photo tapped method

- (void)photoTapped {

if (self.liked == NO) {

      //item has not been liked yet, so you want to like it
      self.liked = YES;
      //call your like method here
      [self like];

    } else {

       //already been liked, now you want to unlike it
       self.liked = NO;
      //call your unlike method here
      [self unlike];

    }

}

is there any other way of creating an if statement because i keep getting these errors

what errors are you getting

was i suppose to make a like property and unlike property ?

was i suppose to make a like property and unlike property ?

you said you had methods for liking a photo and for unliking a photo. you need to call those where I put [self like] and [self unlike]. I dont know what your methods are called, so I just called them like and unlike

its the self.liked = NO; i get errors with and the self.liked = YES;

oh sorry yes you will need to create a bool property for that in your view controller

how would i do that lol?

in your viewController .h file add

@property (nonatomic, assign) BOOL liked;

wow i never used assign as a property before

you cant use strong because strong can only be used with objects and BOOLs are not objects. assign is used mainly for non pointer (plain vanilla primitive types for example) properties

so what i have now checks rot see if the instagram photo is liked and if not it will like it and if it is it will unlike it now I'm trying to figure out why the unlike method doesn't work properly throughout the if statement