Unrecognized selector when using multiple gesture recognizers

Discussion in 'iPhone/iPad Programming' started by tutiplain, May 22, 2011.

  1. macrumors member

    Joined:
    Feb 4, 2011
    #1
    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.
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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.


    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
     
  3. thread starter macrumors member

    Joined:
    Feb 4, 2011
    #3
    THanks, I totally missed that. Problem solved
     

Share This Page