无法将信息传递给以前的ViewController

马里奥:

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

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

我尝试了几种方法来实现,但是没有人正是我想要的。我将粘贴它们中的每一个以及问题的说明:

注意

请注意,我避免使用segue,因此这不是一种选择。

尝试1

导航正确完成。正确的是,它以全屏显示,而不是弹出窗口显示。但是,它并不会保留所做的更改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.尽管如此,它显示为弹出窗口,并且不再显示导航控制器。

@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因此无法更改的值coordinate此外,该ViewController图标显示为弹出窗口而不是全屏显示

@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")
          }
      }
Sh_Khan:

您所有的尝试都将实例化一个新的vc,然后再用标记为let vc = MainViewController()or 的实际呈现的vc进行实例化,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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在iOS swift中将数据传递给以前的viewController?

将值传递给以前的视图控制器不会反映出更改

将参数传递给以Runas启动的程序

如何将String值传递给以下函数

R:将子列表灵活传递给以下功能

C ++,为什么可以将rvalue传递给以lvalue引用为参数的函数

我可以将参数传递给以_.lodash删除的函数吗?

将一个类的列表传递给以抽象基类的列表为参数的方法

将命令行参数传递给以编程方式运行的JUnit测试用例

将快速关闭传递给以块为参数的Objective-C函数

将函数f传递给以f类型为模板的类的构造函数

如何将参数传递给以管理员身份自动运行的脚本?

如何将插槽传递给以编程方式创建的vuejs组件实例

如何将输入传递给以组件为目标的指令

如何将长整型值传递给以整数作为返回类型的函数的参数?

将_bstr_t对象传递给以BSTR作为参数的函数是否安全?

将字典传递给以元组为键的函数时产生关键字错误

如何将参数传递给以字符串形式传递给另一个函数的函数?

将数据传递给ViewController

将CoreData密钥传递给新的ViewController

(iOS + Firebase) 无法将图像从 UITableViewCell 传递给下一个 ViewController

将请求信息传递给Spring AuthenticationProvider

将信息传递给渲染器

将信息传递给指令以匹配密码

SwiftUI:将视图信息传递给 UIKit

将信息传递给UIAlertView委托

将URL中的信息传递给Sharepoint

无法将 ListView 的信息传递给下一个 Activity Android

如何将系统属性传递给以Windows Service运行的Tomcat 7中托管的Web应用程序?