2017-06-29 100 views
0

我想從導航堆棧中刪除以前的視圖控制器。如何從導航控制器ios中刪除以前的視圖控制器?

例如

- > A是根view.now導航到B - B導航到c - c瀏覽到c ​​- c瀏覽到c

- >現在我想刪除所有的C視圖控制器&彈出到B

B未修正視圖控制器。

又如

甲 - >乙 - > C>克> C>˚F> C> C> C> c^

取下導航&需要所有C查看控制器以下輸出

A - >乙 - > C> g>的C>˚F

請幫我

+1

這可能會幫助你:https://stackoverflow.com/a/10281744/4763963 –

+0

我已經檢查this.but不工作 –

+0

我不能理解的是,你如何能夠從c導航到c 。 > A - > b - > c> g> c> f> c> c> c> c – Prashant

回答

0

您可以使用popToViewControllerhttps://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller)。

例如:

在目標C

[self.navigationController popToViewController:self.navigationController.viewControllers[your_VC_index] animated: true]; 
// in first eg your_VC_index will be 1 and another example it will be 5 

在夫特3

self.navigationController.popToViewController(self.navigationController.viewControllers[your_VC_index], animated: true) 
2

確定從導航堆棧控制器。和POP到控制器

NSArray *viewControllers = [[self navigationController] viewControllers]; 
    int count = [viewControllers count]; 

    for (int i= count-2; i >= 0 ; i--) { 

     id obj=[viewControllers objectAtIndex:i]; 

     if(![obj isKindOfClass:[C class]]){ 
      [[self navigationController] popToViewController:obj animated:YES]; 
      return; 
     } 
    } 
+0

通過在導航堆棧中傳遞上面的所有視圖控制器,它將轉到第一個不匹配的視圖控制器。 – suhit

+0

是的,那是他想要的。檢查他的例子@suhit – Subramanian

+0

導航堆棧是'A-> b> c> c> c> c> c> c> c> c'需要的輸出是'A - > b - > c> g> c> f'您的解決方案將顯示ViewController-A,因爲它將停止在第一個索引本身。 – suhit

0

反向疊代導航堆棧並獲得第一個非匹配的viewController的索引,然後popToViewController

if let controllers = self.navigationController?.viewControllers { 
    var count = controllers.count - 1 
    for viewcontroller in controllers.reversed() { 
     if viewcontroller is CViewController { 
      count -= 1 
     } else { 
      break 
     } 
    } 

    let vc = controllers[count] 
    self.navigationController?.popToViewController(vc, animated: true) 
} 
0

寫方法類似於以下,

- (void)removeAllLastCObjectsFromNavigationController { 
    //fetch viewcontroller array. for your case which will be A - > b -> c > g > c > f > c > c > c > c 
    NSArray *viewControllers = yourNavigationController.viewControllers; 
    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray: viewControllers]; 

    while(1) { 
     id viewController = mutableArray.lastObject; 
     if ([viewController isKindOfClass:[YourCClassName class]]) { 
      //Now continue removing the objects from the array if that is C type object. 
      [mutableArray removeLastObject]; 
     } else { 
      //If found any other class than C stop removing viewcontroller 
      break; 
     } 
    } 
    //Now mutableArray contains A - > b -> c > g > c > f 
    yourNavigationController.viewControllers = mutableArray; 
} 
0

由於navigationController的childViewControllers是一個堆棧,
所以使用此功能,您可以彈出到f viewController和「c> c> c> c」被銷燬。

for (UIViewController *tempVC in self.navigationController.childViewControllers) { 
    if ([tempVC isKindOfClass:[fViewController class]]) { 
     fViewController *f = (fViewController *)tempVC; 
     [self.navigationController popToViewController:f animated:YES]; 
    } 
} 

但是,如果你不想彈出到f,只想獲取navigationController的新的childViewControllers。您可以使用下一個功能:

for (UIViewController *tempVC in self.navigationController.childViewControllers) { 
    if ([tempVC isKindOfClass:[ScanViewController class]]) { 
     NSUInteger findex = [self.navigationController.childViewControllers indexOfObject:tempVC]; 
     NSMutableArray *viewArray = [NSMutableArray arrayWithArray:self.navigationController.childViewControllers]; 
     [viewArray removeObjectsInRange:NSMakeRange(findex, viewArray.count - 1)]; 
    } 
} 

的viewArray是新childViewControllers.And如果你有新的需求,可以編輯該功能,然後使用它。

相關問題