Czy można wcisnąć kontroler widoku z blokiem zakończenia?

Dokumentacja UINavigationController nie zawiera metod pushViewController z parametrem completion:.

Author: cbowns, 2014-02-13

6 answers

[[7]] ja bym skorzystał z odpowiedzi @ justMartin . Ale poniżej jest inne podejście.

Push animation take 0.3 seconds.So, jeśli chcesz uruchomić jakąś funkcję po zakończeniu użyj performSelector po 0,3 sekundy opóźnienia.

[self performSelector:@selector(completedPushing:) withObject:nil afterDelay:.3];

Lub
Po wciśnięciu new viewController na navigation stack, old viewController view jest usuwany z widoku hierarchy.so powoduje to wywołanie viewDidDisappear W starym kontrolerze ViewController.Możesz tam napisać swój completion kod. nie zapomnij zadzwonić super w pewnym momencie implementacji viewDidDisappear.

 -9
Author: santhu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-22 17:27:19

Lepszym rozwiązaniem byłoby owinięcie animacji push przez Katransakcję i ustawienie blokady completionBlock. Nie ma potrzeby radzić sobie z czasem.

[CATransaction begin];
[[self navigationController] pushViewController:viewController animated:YES];
[CATransaction setCompletionBlock:^{
    //whatever you want to do after the push
}];
[CATransaction commit];
 59
Author: justMartin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-21 08:52:55

ODPOWIEDŹ Justmartina zadziałała świetnie dla mnie. Dla nowych użytkowników swift API:

CATransaction.begin()
navigationController?.pushViewController(viewController, animated: true)
CATransaction.setCompletionBlock({ 
    //your post animation logic
})
CATransaction.commit()
 6
Author: Ishaan Sejwal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-05 15:13:13

Rozwiązanie z {[1] } jest świetne, ale jest na to inny sposób. Możesz uczynić swój kontroler delegatem UINavigationController i zaimplementować metodę didShowViewController:

class FooBarController: UIViewController, UINavigationControllerDelegate {
  func viewDidLoad() {
    self.navigationController?.delegate = self
  }

  func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
    // your awesome completion code here 
  }
}

Takie podejście może być wygodniejsze dla Twojego zadania

 5
Author: danshevluk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-05 21:01:41

Nie jestem pewien, czy dobrze rozumiem, ale wcisnąłem kontroler widoku w bloku zakończeń w następujący sposób. W kontrolerze widoku tabeli dodano następującą linię do pliku nagłówkowego:

typedef void(^myCompletion)(BOOL);

Następnie w samej klasie dodano następującą metodę:

-(void) myMethod:(myCompletion) compblock
{
    compblock(YES);
}

Teraz w didSelectRowAtIndexPath, zadzwoniłem myMethod i w bloku zakończeń, nacisnąłem kontroler widoku.

[self myMethod:^(BOOL finished) {
    if(finished){
        dispatch_async(dispatch_get_main_queue(), ^{
            DVSecondTableViewController *vc = [[DVSecondTableViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        });
    }
}];

Nie jestem pewien, czy można wypychać Kontrolery widoku poza główny wątek, więc umieściłem to wywołanie w dispatch_async().

 2
Author: Bart van Kuik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-13 03:54:26

Spróbuj użyć -[UIViewControllerTransitionCoordinator animateAlongsideTransitionInView:animation:completion:]:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (!self.isPushing) {
        self.pushing = YES;
        [super pushViewController:viewController animated:animated];
        if (!self.transitionCoordinator) {
            self.pushing = NO;
        } else {
            [self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
                self.pushing = NO;
            }];
        }
    }
}
 0
Author: Lizhen Hu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-23 07:03:47