在 Swift 中的 Nav Controller 中弹出 2 个视图控制器

Pop 2 view controllers in Nav Controller in Swift(在 Swift 中的 Nav Controller 中弹出 2 个视图控制器)
本文介绍了在 Swift 中的 Nav Controller 中弹出 2 个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了很多方法来使用 Objective-C 在 UINavigationController 中弹出 2 个 UIViewControllers,但是当我尝试将其切换到 Swift 时,它似乎没有正在工作.

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 doesn't seem to be working.

返回 UIViewController 的最佳方法是什么?任何指导将不胜感激谢谢

What would be the best approach to pop back to UIViewController? Any guidance would be appreciated Thanks

推荐答案

扩展我的评论,在 viewControllers 数组中找到倒数第二个视图控制器,然后使用 popToViewController 避免覆盖整个视图控制器堆栈.

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];

这篇关于在 Swift 中的 Nav Controller 中弹出 2 个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)