Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tutiplain

macrumors member
Original poster
Feb 4, 2011
95
0
Hi all,

I am experimenting with gesture recognizers, but I've run into a problem I just can't figure out on my own. I have a UIViewcontroller with two swipe gesture recognizers, one for left swipe, one for right swipe. While the right swipe works perfectly, the left swipe throws an unrecognized selector exception, even though, apart from the direction, they are configured exactly the same way. Here's my code:

Here is my interface:
Code:
@interface SecondViewController : UIViewController {
    UISwipeGestureRecognizer *recog_right;  
    UISwipeGestureRecognizer *recog_left;
}

-(void)handleSwipeGesture:(UIGestureRecognizer *)sender;
-(void)handleLeftSwipeGesture:(UIGestureRecognizer *)sender;

@property(nonatomic,retain) UISwipeGestureRecognizer *recog_right;
@property(nonatomic,retain) UISwipeGestureRecognizer *recog_left; 
@end

and my implementation:

Code:
- (void)dealloc
{
    [super dealloc];
    [recog_left release];
    [recog_right release];
}

-(void)handleSwipeGesture:(UIGestureRecognizer *)sender
{
    NSLog(@"Swiped Right!");
}

-(void)handleLeftSwipeGesture:(UIGestureRecognizer *)sender
{
    NSLog(@"Swiped Left!");
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //añade recognizer para la derecha 
    self.recog_right =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self.view addGestureRecognizer:self.recog_right];
    //añade para la izquierda
    self.recog_left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleLeftSwipeGesture:)];
    [self.recog_left setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:self.recog_left];

}

Note that I've only included the methods pertaining what I need to do, it is not the complete implementation of the interface (though most of the others are simly calling their [super].

Questions:
1. Why does this throw unrecognized selector?
2. Why doesn't the setDirection method appear in the docs for UISwipeGestureRecognizer?
3. It seems that it would be possible to set up a single gesture recognizer to handle both directions. How would I know which direction was swiped inside the handler method?

Thanks for any help. It is greatly appreciated.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
Code:
- (void)dealloc
{
    [super dealloc];
    [recog_left release];
    [recog_right release];
}

-(void)handleSwipeGesture:(UIGestureRecognizer *)sender
{
    NSLog(@"Swiped Right!");
}

-(void)[COLOR="red"]handleLeftSwipeGesture[/COLOR]:(UIGestureRecognizer *)sender
{
    NSLog(@"Swiped Left!");
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //añade recognizer para la derecha 
    self.recog_right =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self.view addGestureRecognizer:self.recog_right];
    //añade para la izquierda
    self.recog_left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector([COLOR="red"]hanleLeftSwipeGesture[/COLOR]:)];
    [self.recog_left setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:self.recog_left];

}
Proofread your code better. The red-hilited methods are not spelled the same.

Also, your dealloc is implemented wrong. Reread the section on deallocating objects in the fundamental Memory Management Guide:
http://developer.apple.com/library/...nership.html#//apple_ref/doc/uid/20000043-SW4
Pay particular attention to the placement of [super dealloc] in your dealloc.


2. Why doesn't the setDirection method appear in the docs for UISwipeGestureRecognizer?
Because direction is declared as a property, not a separate setter and getter method. Read the class reference doc for UISwipeGestureRecognizer.
http://developer.apple.com/library/...l#//apple_ref/occ/cl/UISwipeGestureRecognizer
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.