2010-10-21 3 views
3

У меня есть UITableViewController, которому представлен список вариантов. После того, как пользователь нажмет один, я хотел бы вернуться к предыдущему виду. Возврат кажется слишком быстрым с кодом, который я использую. Я хотел бы приостановить на 0.2 секунды или около того, чтобы дать пользователю время, чтобы их выбор стал проверенным. Вот код, который я использую сейчас:Как мне popViewControllerAnimated после задержки?

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

Есть ли хороший способ добавить небольшую задержку до того, как появится контроллер вида?

ответ

9

Надеюсь, что эта помощь.

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

} 

Это дает вам задержку в 0,2 секунды, чтобы вызвать функцию «Увольняющий контроль».

Функция "dismissController".

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

Вы пробовали -performSelector:withObject:afterDelay?

+0

Я пробовал, но у меня были сбои. Проблема (я думаю) заключалась в том, что я пытался сделать все это одним вызовом: '[[self navigationController] выполнитьSelector: @selector (popViewControllerAnimated :) withObject: (id) YES afterDelay: 0.2];'. Что-то пыталось разыскать «ДА». –

-1

сон (0,2) работал на меня

+0

Sleep() работал нормально, но это предотвратило нанесение галочки до завершения сна(). –

+0

Добавить http://github.com/jdg/MBProgressHUD в свой проект и использовать метод: [HUD showWhileExecuting: @selector (sleepTaks) OnTarget: само withObject: ноль анимированный: YES]; Затем он спит, и у вас есть хороший индикатор загрузки. – meersmans

+0

sleep() блокирует вызывающий поток. Вы не должны использовать его в основном потоке в приложении GUI. – alastair

Смежные вопросы