2017-06-24 118 views
1

我有一个使用不同视图控制器的注册流程。我从ViewController1中的用户获取输入,移动到下一个视图控制器(ViewController2),并将数据传递到最后一个视图控制器(例如ViewController5)。将数据传递给另一个ViewController而不移动到ViewController

的问题是,我可以通过我的数据到下一个视图控制器,但我不能把它传递给最后一个视图控制器,如:

// 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。我如何实现这一目标?

回答

2

你可以使用类并创建单独的对象来存储数据,并在第五个视图控制器获取

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) 
+0

这正是我所需要的。很好,谢谢。 – waseefakhtar

0

在你Appdelegate.swift文件中像这样

'var fifthViewController: FifthViewController?' 
在您的视图控制器

从那里,你想传递的数据

'let appDelegate = UIApplication.shared.delegate as! AppDelegate' 

'

appDelegate.fifthViewController = self.storyboard?.instantiateViewController(withIdentifier: “ FifthViewController“) as! FifthViewController 
appDelegate.fifthViewController.dataText = emailText! 

'

创建FifthViewController的实例

当你想要推送FifthViewController时使用Appdelegte refrence像这样的控制器

'self.navigationController?.pushViewController(appDelegate.fifthViewController!, animated: true)' 
+0

只有当我想从发送数据的位置推送视图控制器时,这应该起作用。我不想这样做。 – waseefakhtar

相关问题