2017-08-04 73 views
1

我有兩個導航視圖控制器。當我點擊屬於第二個導航控制器的ViewController中的一個按鈕時,我想關閉該導航控制器的完整視圖控制器堆棧,並且想要轉到第一個導航中的視圖控制器控制器。我該怎麼做?我試過[self.navigationController dismissViewControllerAnimated:YES completion:nil];,似乎沒有發生。怎麼做?如何在導航視圖控制器中關閉整個視圖控制器堆棧?

+0

爲什麼你有兩個導航控制器?他們如何相互關聯? –

+0

我想你可以設置self.navigationController.viewControllers = @ [],然後推動什麼需要。我不知道你是否想要動畫。 – TomCobo

+0

@AndréSlotta他們互不相關 –

回答

1

的錯誤一定在別處。您描述的代碼正在使用確實有效。我創建了一個新項目,並提出一個非常簡單的例子:

#import "ViewController.h" 

@interface MyViewController : UIViewController 
- (instancetype)initWithColor:(UIColor *)color; 
@end 


@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[[MyViewController alloc] initWithColor:[UIColor redColor]]] animated:YES completion:nil]; 
} 

@end 



@implementation MyViewController 

- (instancetype)initWithColor:(UIColor *)color { 
    if((self = [super init])) { 
     self.view.backgroundColor = color; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 
    CGPoint point = [touches.anyObject locationInView:nil]; 
    if(point.x < self.view.frame.size.width*0.5 && point.y < self.view.frame.size.width*0.5) { 
     [self.navigationController pushViewController:[[MyViewController alloc] initWithColor:self.view.backgroundColor] animated:YES]; 
    } else if(point.x > self.view.frame.size.width*0.5 && point.y < self.view.frame.size.width*0.5) { 
     [self.navigationController presentViewController:[[UINavigationController alloc] initWithRootViewController:[[MyViewController alloc] initWithColor:[UIColor greenColor]]] animated:YES completion:nil]; 
    } else if(point.x < self.view.frame.size.width*0.5 && point.y > self.view.frame.size.width*0.5) { 
     [self.navigationController popViewControllerAnimated:true]; 
    } else if(point.x > self.view.frame.size.width*0.5 && point.y > self.view.frame.size.width*0.5) { 
     [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 
    } 
} 

@end 

如果複製到一個新的項目,其中ViewController是您的主視圖控制器導航控制器將創建並呈現在視圖沒有出現在此。背景是紅色的。

通過按屏幕的左上部分,相同顏色的新控制器將被推送到當前頂部導航控制器。

按右上角的按鈕,新的導航控制器將顯示綠色視圖控制器。

按下左下方可以彈出當前視圖控制器(如果有)。

然後按右下角將關閉頂部的導航控制器。

因此,您的情況是按左上角幾次來在單個導航控制器上生成一堆視圖控制器。然後按右上方顯示另一個導航控制器(綠色)。按左上角幾次,在綠色導航控制器上創建一堆視圖控制器。現在按右下角關閉整個綠色堆棧並返回紅色導航控制器堆棧。

檢查你的代碼多一點,看看你的情況是怎麼回事,你爲什麼遇到問題在你的情況。首先檢查self.navigationController是否爲零。

-2

試試這個,我認爲你的作品

self.navigationController?.popToRootViewController(animated: true) 
+0

你是否看到這個問題'我有兩個導航視圖控制器' –

+0

Mitul,這不會工作,因爲我想要顯示的視圖控制器位於另一個導航視圖控制器中。 –

+0

let storyboard = UIStoryboard.init(name:「storyboardname」,bundle:nil) mainNavVC = storyboard.instantiateViewController(withIdentifier:「navigationcontrollername」)as! UINavigationController mainNavVC。setNavigationBarHidden(true,animated:false) mainNavVC.setViewControllers([viewcontrollername],animated:true) self.window?.rootViewController = mainNavVC –

相關問題