0

In my main view, I am collecting data from two other views. I start in the main view, go to the next view to get the data and go back to the original view. Then I go to the last view and get more data, but when I go back the original data is gone. How do I save it before leaving?

I have tried using viewWillDisappear but it wasn't working. Is this a good approach and if so, how do I go about using it?

These are the two pieces of data coming in via segue:

var homeTeamName = String ()
var awayTeamName = String ()

I do this when I load:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.    }
    HomeName.text = homeTeamName
    HomeImage.image = UIImage(named:homeTeamName)
    AwayName.text = awayTeamName
    AwayImage.image = UIImage(named:awayTeamName)

}

Note: Everything with away is coming from one view, and everything with home is coming from another.

Now I am storing like this:

override func viewWillDisappear(_ animated: Bool) {
    UserDefaults.standard.set("value", forKey: "homeTeamName")
}

Retrieving like this:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.    
    UserDefaults.standard.string(forKey: "homeTeamName")
}

Still not working.

Thanks in advance!

Raksha Saini
  • 604
  • 12
  • 28

2 Answers2

0

You cannot the store variable in the ViewController class when you leave it. Everything probably gets destroyed. As you have to store just two strings, the best way to do is to use UserDefaults.

Storing

UserDefaults.standard.set("value", forKey: "homeTeamName") 

Retrieving

UserDefaults.standard.string(forKey: "homeTeamName")

Use this for printing log

print("printingValue\(UserDefaults.standard.string(forKey: "homeTeamName"))")

Hope it Helps!

Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
0

hey write this for data storing

override func viewWillDisappear(_ animated: Bool) {
    let userDefault = UserDefaults.standard
    userDefault.set("value", forKey: "homeTeamName")
}

and Write this for Data Retriving Write this

override func viewWillAppear(_ animated: Bool) {
    let userDefault = UserDefaults.standard
    userDefault.value(forKey: "homeTeamName")
}

i think it helps u.

Mitul.Patel
  • 252
  • 2
  • 8
  • it is preference dictionary which saves data and it will delete when u uninstall application. Userdefaults.standard is preference dictionary. and userdefault is object. and write that code into viewwilldisappear and viewwillappear – Mitul.Patel Aug 12 '17 at 04:20
  • I think I found my problem. When I retrieve with in the viewWillAppear, do I need to do this: homeTeamName = userDefault.value(forKey: "homeTeamName") – Stefan Walzer-Goldfeld Aug 12 '17 at 04:22
  • yea that is problem and always create object of Prefrance so it is better for Programming. – Mitul.Patel Aug 12 '17 at 04:24
  • also, if the userdefault is set the first time it leaves the view, will it change to the new value of homeTeamName when it leaves a second time? – Stefan Walzer-Goldfeld Aug 12 '17 at 04:27
  • it replace with the the second value. – Mitul.Patel Aug 12 '17 at 04:29
  • I don't know how to explain my problem. The view loads and the homeTeamName is blank. I leave to view 2 and when I come back the name has been segued to the original view and works fine. Then I go to view 3 and it disappears. I don't know if this knowledge changes the logic or maybe I have very little understanding of what I'm doing but it isn't working. – Stefan Walzer-Goldfeld Aug 12 '17 at 04:35
  • u can any were access user default. which place u need that value u can create a userdefault object and write that code for retraving u can get that value easily. – Mitul.Patel Aug 12 '17 at 04:39
  • Ackkkk This is beyond my knowledge I'm sorry I don't understand. THanks for the help though. I apreciate it – Stefan Walzer-Goldfeld Aug 12 '17 at 04:43