ios - 将数据传递到另一个 ViewController 而不移动到 ViewController

标签 ios swift uiviewcontroller

我有一个使用不同 View Controller 的注册流程。我在 ViewController1 中获取用户的输入,移动到下一个 View Controller (ViewController2),并将数据传递到最后一个 View Controller (例如 ViewController5)。

问题是我可以将数据传递到下一个 View Controller ,但无法将其传递到最后一个 View Controller ,如下所示:

// Move to Second View of the flow.
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: “SecondViewController“) as! SecondViewController
secondViewController.dataText = dataText!
self.navigationController?.show(secondViewController, sender: nil)

// Pass the data to the last View of the flow.
let fifthViewController = self.storyboard?.instantiateViewController(withIdentifier: “FifthViewController“) as! FifthViewController
fifthViewController.dataText = emailText!

dataText 将传递给 SecondViewController,但不会传递给 FifthViewController。我如何实现这一目标?

最佳答案

您可以使用类并创建单例对象来存储数据和检索 在第五个 View Controller 中

class SingletonClass {
    var sharedInstance: SingletonClass {
          struct Static {
               static let instance = SingletonClass()
          }
          return Static.instance
     }
     var dataText : String = ""
}

现在您可以将数据存储在单例对象中,如下所示

 let singleTon = SingletonClass()
 singleTon.sharedInstance.dataText = "store data"

并在你的 FifthViewController 中像这样使用

  let singleTon = SingletonClass()
  print(singleTon.sharedInstance.dataText)

关于ios - 将数据传递到另一个 ViewController 而不移动到 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736177/

相关文章:

ios - 如何使用SwiftUI使用“PDF”选择器打开“PDF”?

ios - 在 xcode 8 beta 6 中的 UITableView 中使用 indexPath.row 无法编译

ios - Swift 中基于之前 View Controller 的 If 语句

ios - 如何在 swift 3 中将 View Controller 制作为启动画面?

ios - 从 subview Controller 呈现主视图 Controller

iphone - Three20 TTMessageController lite替代品

ios - 这些动画使用了哪些技术?

ios - 在 SwiftUI 中将 @EnvironmentObject 与 PresentationButton 结合使用

ios - UIAlertController 在关闭 WiFi 时出错

html - 导航栏大标题-动画问题