PDA

View Full Version : Unrecognized selector when using multiple gesture recognizers




tutiplain
May 22, 2011, 08:49 AM
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:

@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:


- (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
May 22, 2011, 12:57 PM
- (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];

}



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/ios/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html%23//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/ios/#documentation/uikit/reference/UISwipeGestureRecognizer_Class/Reference/Reference.html%23//apple_ref/occ/cl/UISwipeGestureRecognizer

tutiplain
May 27, 2011, 09:52 PM
THanks, I totally missed that. Problem solved