【问题标题】:Getting previous UIViewController swift快速获取以前的 UIViewController
【发布时间】:2017-06-17 11:39:53
【问题描述】:

我想得到以前的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

【问题讨论】:

  • 你用哪个库来做菜单?
  • SWRevealViewController
  • 好的,您只想在一个地方知道,或者您想从任何文件中知道?
  • 我想知道如何从打开菜单的地方获取 ViewController

标签: ios swift uiviewcontroller


【解决方案1】:

这就是以前viewController的获取方式,查看cmets的解释:

// 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])
        }
    }
}

【讨论】:

  • 我用这种方式打开菜单 let menuButton = UIBarButtonItem.init(title :"open", style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_ :))) navigationItem.leftBarButtonItem = menuButton 所以没有使用导航控制器,这就是 count 返回 nil 的原因,当我尝试您的代码时也没有打印任何内容
  • 这是一个很好的答案,但在这种情况下并没有帮助我我写了我的答案(帮助我的方式)
【解决方案2】:

在 Second ViewController 中添加 First ViewController 类型的变量。

class SecondViewController : UIViewController 
{
   var previousVC = FirstViewController()
}

现在从 First ViewController 到 Second ViewController 执行 segue。

 performSegue(withIdentifier: "to2nd", sender: self) //to2nd or Your Choice

在准备 segue 函数中,将您的 First view Controller 对象放入 target 。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{
        if segue.identifier == "to2nd" // Or you segue id name 
        {
            if let target = segue.destination as? SecondViewController
            {
                target.previousVC = self
            }
        }
}

现在,当您需要以前的 ViewController 或其数据时,请使用 previousVC

【讨论】:

  • 查看我不使用的编辑帖子为 segue 做准备我想在打开菜单的位置获取 UIViewController
  • 你知道如何在objective c var previousVC = FirstViewController()中写这行
【解决方案3】:

如果您的 ViewController 嵌入到 NavigationController 中,您可以在第二个 VC 中使用标准的 popViewController 函数:

_ = navigationController?.popViewController(animated: true)

【讨论】:

    【解决方案4】:

    我解决了我的问题,方法是将 UIViewController 从委托中打开菜单的位置保存,以便我可以从任何地方访问它

    【讨论】:

    • @Rashwan L 这是我的答案
    【解决方案5】:

    斯威夫特 5.x

    假设你在SecondViewController,你想检查之前的视图控制器是否真的是FirstViewController,那么你可以这样做:

    if let prevIndex = self.navigationController?.viewControllers.firstIndex(where: {$0 == self })?.advanced(by: -1) {
       if let prevVC = self.navigationController?.viewControllers[prevIndex], prevVC is FirstViewController {
          // do whatever you want with prevVC
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2018-06-29
      • 2013-03-14
      • 1970-01-01
      相关资源
      最近更新 更多