ios - 快速获取以前的 UIViewController

标签 ios swift uiviewcontroller

我想获取之前的viewController

当我从 navigation 菜单中选择任何行时,我有 2 个 UIViewControllersnavigationMenu 我想从打开菜单的位置获取 UIViewController 以移动到第二个 UIViewController

我想知道在这种情况下如何获取以前的 UIViewController

我试过了,但计数返回 nil:

 let n: Int! = self.navigationController?.viewControllers.count
 print(n)
 let myUIViewController = self.navigationController?.viewControllers[n-2] as! UIViewController

编辑:-

打开菜单代码

  let menuButton = UIBarButtonItem.init(title :"open", style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
    navigationItem.leftBarButtonItem = menuButton

最佳答案

这是获取之前的viewController的方法,查看注释解释:

// get all the viewControllers to start with
if let controllers = self.navigationController?.viewControllers {
    // iterate through all the viewControllers
    for (index, controller) in controllers.enumerated() {
        // when you find your current viewController
        if controller == self {
            // then you can get index - 1 in the viewControllers array to get the previous one
            let myUIViewController = controllers[index-1]
            print(controllers[index-1])
        }
    }
}

关于ios - 快速获取以前的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604182/

相关文章:

ios - iOS 是否支持关联域的通配符? (*.company.com)

ios - 用于发行说明/更新的 App Store API

Swift 中等效的 Java 接口(interface)

swift - 如何计算 NSDictionary 的快速数组的值总和

iphone - 将 UIViewController 代码转换为 UIView

ios - 根据限定符将变量设置为不同的自定义 View (Swift)

ios - 禁用实时应用程序的 TestFlight 检查点

iphone - 尝试构建存档时尝试获取 Apple Mach-O 链接器错误

ios - 使呈现的 View Controller 忽略并传递触摸事件

iOS - 将数据传递给推送的 View Controller