10

我在調用我的方法dismissView時遇到此錯誤。這裏是方法存根:'試圖彈出到不存在的視圖控制器。'

-(IBAction)dismissView 
{ 
    RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
    [self.navigationController popToViewController:rootController animated:YES]; 
} 

這應該工作,我檢查,rootController初始化和分配。有任何想法嗎?

回答

20

最近我有這個問題,像這樣的東西解決了......

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]; 
+0

感謝,適用於我的情況 – tesmojones 2014-06-05 07:34:57

+0

迅速實現3'讓_ = self.navigationController .popToViewController((self.navigationController .viewControllers [1])作爲HomeViewController,動畫:?!真)?' – 2016-10-25 06:55:15

10

-popToViewController用於將視圖控制器關閉堆棧,直至已經存在的視圖控制器。您的UINavigationController有一堆ViewControllers(存儲在viewControllers屬性中),當您要popToViewController時,您將要傳遞該數組中的其中一個元素作爲第一個參數。

你最有可能想在這種情況下,做的是使用-popViewControllerAnimated:,這將從堆棧

4

你分配RootViewController的右邊有去除頂部ViewController。它並不存在於導航控制器的堆棧中,所以無論你彈出多遠,都不會觸及它。

10

我解決了這個用pushViewController而不是popToViewController

4

如果您正在使用Storyboads,使用此賽格瑞:

#import "PopToControllerSegue.h" 

@implementation PopToControllerSegue 

- (void) perform 
{ 
    UIViewController *sourceViewController = (UIViewController *)self.sourceViewController; 
    UIViewController *destinationViewController = (UIViewController *)self.destinationViewController; 

    for (UIViewController* controller in sourceViewController.navigationController.viewControllers) { 
     if ([controller isKindOfClass:destinationViewController.class]) { 
      [sourceViewController.navigationController popToViewController:controller animated:YES]; 
      return; 
     } 
    } 

    NSLog(@"PopToControllerSegue has failed!"); 
} 

@end 
0

使用Push Segues時,您可以使用此方法輕鬆回到根目錄:

[self.navigationController popToRootViewControllerAnimated:YES]; 

當使用模態塞格斯(因爲這個詞駁回問題的和作爲一般參考),則可以使用此方法dismiss視圖控制器:

[self dismissViewControllerAnimated:YES completion:nil]; 
1

UINavigationController具有的堆ViewControllers它存儲在viewControllers(NSArray)屬性中。列舉到要求的ViewController並彈出到該ViewController

以下代碼應該解決問題。

-(IBAction)dismissView 
{ 
    NSArray *array = self.navigationController.viewControllers; 
    for (id controller in array) { 
     if ([controller isKindOfClass:[RootViewController class]]) { 
      [self.navigationController popToViewController:controller animated:YES]; 

     } 
    } 
}