iphone - 从导航堆栈中删除 View Controller

标签 iphone ios uiviewcontroller uinavigationcontroller

我有一个导航堆栈,其中包含 5 个 UIViewController。我想通过单击第 5 个 View Controller 中的按钮来删除堆栈中的第 3 个和第 4 个 View Controller 。是否有可能做到这一点?如果是怎么办?

最佳答案

使用此代码并享受:

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

希望对您有所帮助。

编辑: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

编辑:删除除最后一个之外的所有 ViewController -> 左上角没有后退按钮

guard let navigationController = self.navigationController else { return }
var navigationArray = navigationController.viewControllers // To get all UIViewController stack as Array
let temp = navigationArray.last
navigationArray.removeAll()
navigationArray.append(temp!) //To remove all previous UIViewController except the last one
self.navigationController?.viewControllers = navigationArray

关于iphone - 从导航堆栈中删除 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10281545/

相关文章:

iphone - 具有半透明选定背景的 UITableViewCell

javascript - Html5 音频标签在 iOS7 设备上不起作用

IOS UINavigationController、pushViewController 不工作

ios - 从 ViewController 内部调用 MainViewController 的方法 didselectrowatindexpath Objective c

iphone - JSON 解析未提供正确的 NSMutable 数组 :

ios - 当按下按钮时 UIAlertController 被关闭时得到通知

iphone - 如何在同一 map View 中将不同图像添加到注释

iphone - xcode 工具查找内存问题,访问指向另一个变量 ecc 位置的指针

iphone - 如何: Flipboard Navigation Animation (Not the Page Flip)

ios - 从 CKRecord 获取 CKRecordZone