How do I popViewControllerAnimated after a delay?

I have a UITableViewController which is presented with a list of options. After the user clicks one, I would like to return to the previous view. The return seems too fast with the code I use. I would like to pause for 0.2 seconds or so, to give the user time to make their selection checked. Here is 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 a "none" selection, we don't deselect if the user taps the one that 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 appears?

+3
source share
3 answers

I hope for this help.

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

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Since there a "none" selection, we don't deselect if the user taps the one that 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];

}

0,2- "rejectController".

"rejectController".

- (void) dismissController {
  [[self navigationController] popViewControllerAnimated:YES];
}
+9

-performSelector:withObject:afterDelay?

+5

sleep (0.2) worked for me

-1
source

Source: https://habr.com/ru/post/1770765/


All Articles