views:

62

answers:

4

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?

+5  A: 

Have you tried -performSelector:withObject:afterDelay ?

highlycaffeinated
I had tried it, but I was getting crashes. The problem (I think) was that I was trying to do it all in one call: `[[self navigationController] performSelector:@selector(popViewControllerAnimated:) withObject:(id)YES afterDelay:0.2];`. Something was trying to dereference `YES`.
Jay Goodman Tamboli
A: 

Try sleep(x);

- (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]];     
    }

sleep(1);
[[self navigationController] popViewControllerAnimated:YES];
}

that might get you what you want...

Geoff Baum
A: 

sleep(0.2) worked for me

meersmans
Sleep() worked OK, but it prevented the drawing of the checkmark until after the sleep() finished.
Jay Goodman Tamboli
Add http://github.com/jdg/MBProgressHUD to your project, and use the method: [HUD showWhileExecuting:@selector(sleepTaks) onTarget:self withObject:nil animated:YES]; Then it sleeps and you have a nice Loading indicator
meersmans
+2  A: 

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];
}
dianz