Zdobywanie najbardziej UIViewController

Jeśli naciskam Kontrolery widoku i / lub prezentuję Kontrolery widoku modalnego na UINavigationController, Jak mogę dowiedzieć się, co jest najbardziej top UIViewController? Lub w moim przypadku, chcę wiedzieć, czy pewne UITableViewController jest najbardziej, czy nie.

Próbowałem użyć:

self.navigationController.topViewController == self

... ale to nie działa. Domyślam się, że to zawodzi, ponieważ prezentuję Kontrolery widoku modalnego na górze i że topViewController tylko śledzi, które widoki zostały popchnięte na UINavigationController (w przeciwieństwie do tych, które były prezentowany modalnie).

Author: KlimczakM, 2010-11-01

3 answers

Chcesz visibleViewController:

Aktualnie widoczny Widok może należeć do kontrolera widoku u góry stosu nawigacji lub do kontrolera widoku, który został przedstawiony modalnie.

 97
Author: Adam Ernst,
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
2010-11-01 12:31:54
NSArray *viewContrlls=[[self navigationController] viewControllers];

[viewContrlls lastObject];
 22
Author: Swastik,
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
2012-07-25 13:07:28

Wiem, że pytanie jest stare, ale wciąż popularne - dlatego chciałbym opublikować moje najlepsze rozwiązanie, które obsługuje różne podklasy UIViewController's. Jednocześnie możesz rozszerzyć funkcjonalność tej metody o własne Kontrolery "collection", takie jak menu boczne.

extension UIWindow {

  var visibleViewController: UIViewController? {
    guard let rootViewController = rootViewController else {
      return nil
    }
    return visibleViewController(for: rootViewController)
  }

  private func visibleViewController(for controller: UIViewController) -> UIViewController {
    var nextOnStackViewController: UIViewController? = nil
    if let presented = controller.presentedViewController {
      nextOnStackViewController = presented
    } else if let navigationController = controller as? UINavigationController,
      let visible = navigationController.visibleViewController {
      nextOnStackViewController = visible
    } else if let tabBarController = controller as? UITabBarController,
      let visible = (tabBarController.selectedViewController ??
        tabBarController.presentedViewController) {
      nextOnStackViewController = visible
    }

    if let nextOnStackViewController = nextOnStackViewController {
      return visibleViewController(for: nextOnStackViewController)
    } else {
      return controller
    }
  }

}
 0
Author: Timur Bernikovich,
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
2017-12-13 15:55:23