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
Mcgerald Lezeau
7,096 Pointsdoes 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
Stone Preston
42,016 Pointswell 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
}
Mcgerald Lezeau
7,096 Pointswhat 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
Stone Preston
42,016 Pointswell 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];
}
}
Mcgerald Lezeau
7,096 Pointsis there any other way of creating an if statement because i keep getting these errors
Stone Preston
42,016 Pointswhat errors are you getting
Mcgerald Lezeau
7,096 Pointswas i suppose to make a like property and unlike property ?
Mcgerald Lezeau
7,096 Pointswas i suppose to make a like property and unlike property ?
Stone Preston
42,016 Pointsyou 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
Mcgerald Lezeau
7,096 Pointsits the self.liked = NO; i get errors with and the self.liked = YES;
Stone Preston
42,016 Pointsoh sorry yes you will need to create a bool property for that in your view controller
Mcgerald Lezeau
7,096 Pointshow would i do that lol?
Stone Preston
42,016 Pointsin your viewController .h file add
@property (nonatomic, assign) BOOL liked;
Mcgerald Lezeau
7,096 Pointswow i never used assign as a property before
Stone Preston
42,016 Pointsyou 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
Mcgerald Lezeau
7,096 Pointsso 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