Pop 2 view controllers in Nav Controller in Swift

后端 未结 8 1007
星月不相逢
星月不相逢 2021-01-30 12:46

I have found many ways to pop back 2 UIViewControllers in UINavigationController using Objective-C, however when I try and switch that over to Swift it

8条回答
  •  迷失自我
    2021-01-30 13:33

    Expanding on my comment, find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.

    Example (assumes the navigation controller has more than 1 view controller):

    func backTwo() {
        let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
        self.navigationController!.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
    }
    

    Objective-C

    NSArray *viewControllers = [self.navigationController viewControllers];
    [self.navigationController popToViewController:viewControllers[viewControllers.count - 3] animated:YES];
    

提交回复
热议问题