Unrecognized selector when using multiple gesture recognizers

Discussion in 'iOS Programming' started by tutiplain, May 22, 2011.

  1. macrumors member

    Feb 4, 2011
    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; 
    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].

    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 604

    Aug 9, 2009
    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:
    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.
  3. thread starter macrumors member

    Feb 4, 2011
    THanks, I totally missed that. Problem solved

Share This Page