Search code examples
uitableviewxcode4uigesturerecognizeruiswipegesturerecognizer

UIGestureRecognizer Issue: trying to navigate backwards


Trying to make it so if the user swipes left, the app will navigate backwards. Lately the only success I've had is if I specify the UITableView; if a cell is already selected and then a swipe occurs WITHIN the cell it will navigate backwards, but that's it. I'd like to make it so the user can swipe anywhere on the screen without being confined to swiping a value in a table cell. After they've done so, the initial array menu is displayed. Any feedback? Code below:

- (void)viewDidLoad
{
    [super viewDidLoad];
    arrayNo = [[NSMutableArray alloc] init];
    [[self myTableView] setDelegate:self];
    [[self myTableView] setDataSource:self];

    UISwipeGestureRecognizer * back = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reLoad)];
    [back setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.view addGestureRecognizer:back];

    if (back)
    {
        NSLog(@"SWIPED");
        NSString *arts = @"Arts and Museums";
        NSString *coffee = @"Coffee and Bakeries";
        NSString *tours = @"Tours and Festivals";
        NSString *hotels = @"Hotels and Inns";
        NSString *leisure = @"Leisure and Recreation";
        NSString *music = @"Live Music";
        NSString *bars = @"Night Clubs and Bars";
        NSString *food = @"Restaurants";
        NSString *shopping = @"Shopping";
        NSString *transportation = @"Transportation";

        [arrayNo removeAllObjects];

        [arrayNo addObject:arts];
        [arrayNo addObject:coffee];
        [arrayNo addObject:tours];
        [arrayNo addObject:hotels];
        [arrayNo addObject:leisure];
        [arrayNo addObject:music];
        [arrayNo addObject:bars];
        [arrayNo addObject:food];
        [arrayNo addObject:shopping];
        [arrayNo addObject:transportation];
    }
}

Solution

  • I do believe I was making things too complicated. To solve, I am now using this:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        UISwipeGestureRecognizer * back = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backUp1)];
        [back setDirection:(UISwipeGestureRecognizerDirectionLeft)];
        [self.view addGestureRecognizer:back];
    
        if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
        {        
            NSString *galleries = @"Art Galleries";
            NSString *dramatic = @"Dramatic Arts";
            NSString *museums = @"Museums";
    
            [arrayNo removeAllObjects];
    
            [arrayNo addObject:galleries];
            [arrayNo addObject:dramatic];
            [arrayNo addObject:museums];
    
            [self.myTableView reloadData];
        }
    }
    
    -(void)backUp1
    {
        NSLog(@"SWIPED");
        NSString *arts = @"Arts and Museums";
        NSString *coffee = @"Coffee and Bakeries";
        NSString *tours = @"Tours and Festivals";
        NSString *hotels = @"Hotels and Inns";
        NSString *leisure = @"Leisure and Recreation";
        NSString *music = @"Live Music";
        NSString *bars = @"Night Clubs and Bars";
        NSString *food = @"Restaurants";
        NSString *shopping = @"Shopping";
        NSString *transportation = @"Transportation";
    
        [arrayNo removeAllObjects];
    
        [arrayNo addObject:arts];
        [arrayNo addObject:coffee];
        [arrayNo addObject:tours];
        [arrayNo addObject:hotels];
        [arrayNo addObject:leisure];
        [arrayNo addObject:music];
        [arrayNo addObject:bars];
        [arrayNo addObject:food];
        [arrayNo addObject:shopping];
        [arrayNo addObject:transportation];
    
        [self.myTableView reloadData];
    }