无法将信息传递给上一个ViewController

Unable to pass information to the previous ViewController

我知道关于这个主题的问题和答案很少,但是请相信我,我在发布这个问题之前已经检查过它们。

我正在尝试将信息传递给控制器​​(显示的是上一个控制器)。总而言之,我正在尝试将信息从第二个控制器 SearchViewController 发送到第一个控制器 MainViewController(嵌入在导航控制器中)。

我已经尝试了几种方法,但没有一种是我正在寻找的。我会一一贴上,以及问题的解释:

备注

请注意,我避免使用 segue,所以它不是一个选项。

尝试 1

导航正确。它显示为全屏,而不是弹出 window,这是正确的。尽管如此,它并没有保留对 coordinate 所做的更改,而是采用默认值,这让我相信它是一个新实例,而不是我要导航到的实例。

@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
      // Set the new values of from/to coordinates and navigates to the main controller
    if((self.coordinate) {
          // Full screen. Navigation controller shown. Does not keep the changes in variables
          let vc = MainViewController()
          vc.setCoordinate(coordinate: self.coordinate!)
          navigationController?.popToRootViewController(animated: true)

      } else {
          showToast(message: "Please, enter the coordinate")
      }
  }

尝试 2 导航正确并保留了 coordinate. 上所做的更改,但是,它显示为弹出 window 并且导航控制器不再显示。

@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
      // Set the new values of from/to coordinates and navigates to the main controller
    if((self.coordinate) {
          // Pop window. Not full screen. Navigation controller not shown. Keeps the variable values
         let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
         let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
         mainViewController.setCoordinate(fcoordinate: coordinate!)
         self.present(mainViewController, animated: true, completion: nil)

      } else {
          showToast(message: "Please, enter the coordinate")
      }
  }

尝试 3

我使用这种方法是因为我试图返回到之前的控制器,但是 dismiss 没有导航到任何地方,所以它失败了。

@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
      // Set the new values of from/to coordinates and navigates to the main controller
    if((self.coordinate) {
         // Does not come back to the previous controller
         let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
         let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
         mainViewController.setCoordinate(coordinate: coordinate!)
         self.dismiss(animated: true, completion: nil)

      } else {
          showToast(message: "Please, enter the coordinate")
      }
  }

尝试 4

这种方法不允许我访问 mainViewController 的 methods/variables,所以我无法更改 coordinate 的值。此外,ViewController 显示为弹出 window 而不是全屏

@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
      // Set the new values of from/to coordinates and navigates to the main controller
    if((self.coordinate) {
         // Pop window. Not full screen. No access to methods
         guard let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainViewControllerID") else { return }
         let navController = UINavigationController(rootViewController: vc)
         self.navigationController?.present(navController, animated: true, completion: nil)

      } else {
          showToast(message: "Please, enter the coordinate")
      }
  }

尝试 5

导航正确。它是全屏的,很好,但是没有显示导航控制器。它不保留在 coordinate

中所做的更改
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
          // Set the new values of from/to coordinates and navigates to the main controller
        if((self.coordinate) {
            // Full screen. Navigation controller shown. Does not keep the changes in variables
            let vc = MainViewController()
            vc.setCoordinate(coordinate: self.coordinate!)
            self.navigationController?.popViewController(animated: true)

          } else {
              showToast(message: "Please, enter the coordinate")
          }
      }

你所有的尝试都实例化了一个新的 vc 而不是真正呈现的前一个标有 let vc = MainViewController()storyBoard.instantiateViewController 的实例,所以你需要

let arr = self.navigationController!.viewControllers
let prev = arr[arr.count - 2] as! MainViewController
prev.setCoordinate(coordinate: self.coordinate!)
self.navigationController!.popToRootViewController(animated: true)