1

I have 6 view controllers, where you input your name on screen 2, and i want it to carry through so I can put the users name that they input as apart of an email body. Say you input your name as "Kevin", then on screen 5 where you click send, the email display comes up and the subject has "Name: 'Kevin'" and so on. Theres no tutorials on how to do this, only for view controllers that are side by side, and this one has to pass data from 2 to 5.

Ika Venne
  • 35
  • 1
  • 6

3 Answers3

1

You can UserDefault

In Screen 2

set value

 UserDefaults.standard.setValue("your user name", forKey: "UserName")
 UserDefaults.standard.synchronize()

In screen 5

 let strUserName =  UserDefaults.standard.value(forKey:"UserName") // this your input

You need to remove value from UserDefault while it not required

UserDefaults.standard.setValue(nil, forKey: "UserName")

OR

 UserDefaults.removeObject(forKey: "UserName")

 UserDefaults.standard.synchronize()
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • where would i input this? under viewDidLoad or under class? also what code would i have to type in to get it to be included in the email body? Im using a UITextField so would need to pass what ever the user inputs – Ika Venne Jun 02 '17 at 09:09
  • you can put in viewDidDisappear or textFieldDidEndEditing. – KKRocks Jun 02 '17 at 09:10
  • let strUserName is your input value then you need to set as email body. – KKRocks Jun 02 '17 at 09:12
  • Using `synchronize()` has been useless since iOS8. It's explained in the documentation. – Eric Aya Jun 02 '17 at 09:35
1

What about creating a singleton class and store the date in it? Here is one example for it:

Singleton with properties in Swift 3

0

The easy way to just share String is to use UserDefaults. You can save something to it and get it on another screen.

UserDefaults.standard.setValue(value, forKey: key)
UserDefaults.standard.value(forKey: key)
Michał Kwiecień
  • 2,734
  • 1
  • 20
  • 23