ios 从导航堆栈中删除视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10281545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:00:11  来源:igfitidea点击:

Removing viewcontrollers from navigation stack

iphoneiosuiviewcontrolleruinavigationcontroller

提问by Jean Paul Scott

I have a navigation stack, with say 5 UIViewControllers. I want to remove the 3rd and 4th viewcontrollers in the stack on the click of a button in the 5th viewcontroller. Is it possible to do this? If so how?

我有一个导航堆栈,比如 5 个 UIViewController。我想在单击第 5 个视图控制器中的按钮时删除堆栈中的第 3 个和第 4 个视图控制器。是否有可能做到这一点?如果是这样怎么办?

回答by Nitin

Use this code and enjoy:

使用此代码并享受:

NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];

// [navigationArray removeAllObjects];    // This is just for remove all view controller from navigation stack.
[navigationArray removeObjectAtIndex: 2];  // You can pass your index here
self.navigationController.viewControllers = navigationArray;
[navigationArray release];

Hope this will help you.

希望这会帮助你。

Edit: Swift Code

编辑:Swift 代码

guard let navigationController = self.navigationController else { return }
var navigationArray = navigationController.viewControllers // To get all UIViewController stack as Array
navigationArray.remove(at: navigationArray.count - 2) // To remove previous UIViewController
self.navigationController?.viewControllers = navigationArray

回答by Sourabh Bhardwaj

You can first get all the view controllers in the array and then after checking with the corresponding view controller class, you can delete the one you want.

您可以先获取数组中的所有视图控制器,然后与相应的视图控制器类核对后,您可以删除您想要的那个。

Here is small piece of code:

这是一小段代码:

NSArray* tempVCA = [self.navigationController viewControllers];

for(UIViewController *tempVC in tempVCA)
{
    if([tempVC isKindOfClass:[urViewControllerClass class]])
    {
        [tempVC removeFromParentViewController];
    }
}

I think this will make your work easier.

我认为这会让你的工作更轻松。

回答by kuzdu

Swift 3 & 4/5

斯威夫特 3 & 4/5

self.navigationController!.viewControllers.removeAll()

self.navigationController!.viewControllers.removeAll()

self.navigationController?.viewControllers.remove(at: "insert here a number")

self.navigationController?.viewControllers.remove(at: "insert here a number")

Swift 2.1

斯威夫特 2.1

remove all:

移除所有:

self.navigationController!.viewControllers.removeAll()

self.navigationController!.viewControllers.removeAll()

remove at index

在索引处删除

self.navigationController?.viewControllers.removeAtIndex("insert here a number")

self.navigationController?.viewControllers.removeAtIndex("insert here a number")

There a bunch of more possible actions like removeFirst,range etc.

还有更多可能的操作,例如 removeFirst、range 等。

回答by Niklas

Swift 5:

斯威夫特 5:

navigationController?.viewControllers.removeAll(where: { (vc) -> Bool in
    if vc.isKind(of: MyViewController.self) || vc.isKind(of: MyViewController2.self) {
        return false
    } else {
        return true
    }
})

回答by Thein

Using setViewControllersfunction from UINavigationControlleris the best way. There is also animatedparameter to enable animation.

使用setViewControllers函数 fromUINavigationController是最好的方法。还有一个animated参数可以启用动画。

func setViewControllers(_ viewControllers: [UIViewController], animated: Bool)

Example in swift for question

swift 中的示例问题

func goToFifthVC() {

    var currentVCStack = self.navigationController?.viewControllers
    currentVCStack?.removeSubrange(2...3)

    let fifthVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "fifthVC")
    currentVCStack?.append(fifthVC)

    self.navigationController?.setViewControllers(currentVCStack!, animated: true)
}

I tried other ways like [tempVC removeFromParentViewController];. It make weird behaviour, removed ViewController navigation still showing when pop back like reported by @robin-ellerkmann

我尝试了其他方法,例如[tempVC removeFromParentViewController];. 它产生了奇怪的行为,删除了 ViewController 导航仍然显示弹出时如@robin-ellerkmann 报告的那样

回答by tahir raees

Swift 2.0:

斯威夫特 2.0:

  var navArray:Array = (self.navigationController?.viewControllers)!
  navArray.removeAtIndex(navArray.count-2)
  self.navigationController?.viewControllers = navArray

回答by Mitchell C

Swift 5, Xcode 11.3

斯威夫特 5,Xcode 11.3

I found this approach simple by specifying which view controller(s) you want to remove from the navigation stack.

通过指定要从导航堆栈中删除的视图控制器,我发现这种方法很简单。

extension UINavigationController {

    func removeViewController(_ controller: UIViewController.Type) {
        if let viewController = viewControllers.first(where: { 
navigationController.removeViewController(YourViewController.self)
.isKind(of: controller.self) }) { viewController.removeFromParent() } } }

Example use:

使用示例:

secondViewController =  [self.navigationController.viewControllers objectAtIndex:yourViewControllerIndex];

回答by Vignesh

If you are trying to move to 2nd view controller from 5th view controller (skipping 3rd and 4th), you would like to use [self.navigationController popToviewController:secondViewController].

如果您尝试从第 5 个视图控制器(跳过第 3 个和第 4 个)移动到第 2 个视图控制器,您希望使用[self.navigationController popToviewController:secondViewController].

You can obtain the secondViewControllerfrom the navigation controller stack.

您可以secondViewController从导航控制器堆栈中获取 。

if let navVCsCount = navigationController?.viewControllers.count {
    navigationController?.viewControllers.removeSubrange(Range(2..<navVCsCount - 1))
}

回答by Nikola Markovic

Use this

用这个

let VCCount = self.navigationController!.viewControllers.count
self.navigationController?.viewControllers.removeSubrange(Range(VCCount-3..<VCCount - 1))

It will take care of ViewControllers of navigationController. viewControllers and also a navigationItems stacked in navigationBar.

它将处理 navigationController 的 ViewControllers。viewControllers 和一个 navigationItems 堆叠在导航栏中。

Note: Be sure to call it at least after viewDidAppear

注意:一定要至少在 viewDidAppear 之后调用

回答by babak

This solution worked for me in swift 4:

这个解决方案在 swift 4 中对我有用:

self.navigationController!.viewControllers.count - 1

your current view controller index in stack is:

您当前在堆栈中的视图控制器索引是:

##代码##