Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I popViewControllerAnimated after a delay?

I have a UITableViewController that is presented with a list of choices. After the user taps one, I'd like to return to the previous view. The return seems too quick with the code I'm using though. I'd like to pause for 0.2 seconds or so to give the user time to see their selection become checked. Here's the code I'm using now:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger oldSelection = [[selectedCriteria objectAtIndex:criteriaSection] integerValue];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // Since there's a "none" selection, we don't deselect if the user taps the one that's already selected
    if ([indexPath row] != oldSelection + 1) {
        NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:oldSelection+1 // Shift down for "None"
                                                inSection:[indexPath section]];
        UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
        [checkedCell setAccessoryType:UITableViewCellAccessoryNone];

        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
        [selectedCriteria replaceObjectAtIndex:criteriaSection
                                    withObject:[NSNumber numberWithUnsignedInteger:[indexPath row]-1]];     
    }

    [[self navigationController] popViewControllerAnimated:YES];
}

Is there a good way to add a short delay before the view controller is popped?

like image 897
Jay Goodman Tamboli Avatar asked Dec 10 '22 13:12

Jay Goodman Tamboli


2 Answers

Hope this help.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSUInteger oldSelection = [[selectedCriteria objectAtIndex:criteriaSection] integerValue];

[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Since there's a "none" selection, we don't deselect if the user taps the one that's already selected
if ([indexPath row] != oldSelection + 1) {
    NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:oldSelection+1 // Shift down for "None"
                                            inSection:[indexPath section]];
    UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
    [checkedCell setAccessoryType:UITableViewCellAccessoryNone];

    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
    [selectedCriteria replaceObjectAtIndex:criteriaSection
                                withObject:[NSNumber numberWithUnsignedInteger:[indexPath row]-1]];     
}

[self performSelector:@selector(dismissController) withObject:nil afterDelay:0.2];

}

This give you a 0.2 seconds delay to call the function "dismissController".

The function "dismissController".

- (void) dismissController {
  [[self navigationController] popViewControllerAnimated:YES];
}
like image 60
DLende Avatar answered Jan 01 '23 08:01

DLende


Have you tried -performSelector:withObject:afterDelay ?

like image 44
highlycaffeinated Avatar answered Jan 01 '23 08:01

highlycaffeinated