2012-07-31 23 views
6

Chcę przedstawić modalviewcontroller po każdym push-Komunikat otrzyma aplikacji w "application: (UIApplication *) didReceiveRemoteNotification zastosowanie: (NSDictionary *) userinfo"występuje więcej niż jeden modalview w appdelegate

przedstawiam viewcontroller jak to:

ReleaseViewController *viewController = [[ReleaseViewController alloc] init]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self.window.rootViewController presentModalViewController:navController animated:YES]; 

Więc kiedy nadejdzie kolejna wiadomość push-a stary modalViewController wciąż visibile, chcę przedstawić nowy modalviewcontroller nad starym. Ale to nie działa. Nic się nie stało, a konsola po prostu mówi (myślę, że to debug-przesłanie iOS 6 beta):

Warning: Attempt to present <UINavigationController: 0x1dde6c30> on <UINavigationController: 0x1dd73c00> whose view is not in the window hierarchy! 

Co robię źle?

PS: Nie chcę odrzucać starego kontrolera ViewController, chcę, żeby były ułożone.

Dzięki!

Odpowiedz

11

Można uzyskać szczyt Pana zdaniem kontrolerów, a następnie przedstawienie nowego modalnego z tym górnym widoku kontrolera

- (UIViewController *)topViewController:(UIViewController *)rootViewController 
{ 
    if (rootViewController.presentedViewController == nil) { 
     return rootViewController; 
    } 

    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) { 
     UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController; 
     UIViewController *lastViewController = [[navigationController viewControllers] lastObject]; 
     return [self topViewController:lastViewController]; 
    } 

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController; 
    return [self topViewController:presentedViewController]; 
} 

Można wywołać tę metodę z rootViewController jest okna rootViewController

+0

działa idealnie! wielkie dzięki! :) – CrEaK

+0

Świetne rozwiązanie, dzięki. – NSTJ

0

Tutaj jest taki sam jak wyżej , ale napisany w Swift

private func topViewController() -> UIViewController { 
    var rootViewController = UIApplication.sharedApplication().keyWindow!.rootViewController! 
    repeat { 
     if rootViewController.presentingViewController == nil { 
      return rootViewController 
     } 
     if let navigationController = rootViewController.presentedViewController as? UINavigationController { 
      rootViewController = navigationController.viewControllers.last! 
     } 
     rootViewController = rootViewController.presentedViewController! 
    } while true 
} 
0

Pełna Godna był blisko, ale miał kilka błędów, które powodują powrót niewłaściwy kontroler widoku w niektórych przypadki. Oto poprawiona wersja.

private func topViewController(rootViewController: UIViewController) -> UIViewController { 
    var rootViewController = UIApplication.sharedApplication().keyWindow!.rootViewController! 
    repeat { 
     guard let presentedViewController = rootViewController.presentedViewController else { 
      return rootViewController 
     } 

     if let navigationController = rootViewController.presentedViewController as? UINavigationController { 
      rootViewController = navigationController.topViewController ?? navigationController 

     } else { 
      rootViewController = presentedViewController 
     } 
    } while true 
} 
Powiązane problemy