Reputation: 13
I just added a catch and release button. I want to save the button output so that I can exit the app and reopen it with the saved button output. here's my code
import UIKit
class PokemonViewController: UIViewController {
var url: String!
var pokeCatch = false
var currentPokemon: Int = 0
var caughtPokemon: [Int] = []
@IBOutlet var nameLabel: UILabel!
@IBOutlet var numberLabel: UILabel!
@IBOutlet var type1Label: UILabel!
@IBOutlet var type2Label: UILabel!
@IBOutlet var catchLabel: UIButton!
func capitalize(text: String) -> String {
return text.prefix(1).uppercased() + text.dropFirst()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
nameLabel.text = ""
numberLabel.text = ""
type1Label.text = ""
type2Label.text = ""
loadPokemon()
}
func loadPokemon() {
URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
guard let data = data else {
return
}
do {
let result = try JSONDecoder().decode(PokemonResult.self, from: data)
DispatchQueue.main.async {
self.navigationItem.title = self.capitalize(text: result.name)
self.nameLabel.text = self.capitalize(text: result.name)
self.numberLabel.text = String(format: "#%03d", result.id)
for typeEntry in result.types {
if typeEntry.slot == 1 {
self.type1Label.text = typeEntry.type.name
}
else if typeEntry.slot == 2 {
self.type2Label.text = typeEntry.type.name
}
}
}
}
catch let error {
print(error)
}
}.resume()
}
@IBAction func toggleCatch() {
if pokeCatch == true{
pokeCatch = false
catchLabel.setTitle("Catch", for: .normal)
caughtPokemon.append(currentPokemon)
let defaults = UserDefaults.standard
defaults.set(caughtPokemon, forKey: "caughtPokemon")
} else {
pokeCatch = true
catchLabel.setTitle("Release", for: .normal)
if let index = caughtPokemon.firstIndex(of: currentPokemon) {
caughtPokemon.remove(at: index)
}
let defaults = UserDefaults.standard
defaults.set(caughtPokemon, forKey: "caughtPokemon")
}
}
}
Upvotes: 1
Views: 69
Reputation: 4953
If i understand your requirement correctly. You need to be in the same toggle state that the user was when app was killed. For that, you can save the pokeCatch boolean also to Userdefaults and set the state after app relaunch depending on that
@IBAction func toggleCatch() {
if pokeCatch == true{
pokeCatch = false
catchLabel.setTitle("Catch", for: .normal)
caughtPokemon.append(currentPokemon)
let defaults = UserDefaults.standard
defaults.set(caughtPokemon, forKey: "caughtPokemon")
} else {
pokeCatch = true
catchLabel.setTitle("Release", for: .normal)
if let index = caughtPokemon.firstIndex(of: currentPokemon) {
caughtPokemon.remove(at: index)
}
let defaults = UserDefaults.standard
defaults.set(caughtPokemon, forKey: "caughtPokemon")
}
// Save the current state to user default
defaults.set(pokeCatch, forKey: "pokemonCatchState")
}
Then in viewWillAppear you can read the state and configure your UI accordingly.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
nameLabel.text = ""
numberLabel.text = ""
type1Label.text = ""
type2Label.text = ""
loadPokemon()
// Restore Previous state
let defaults = UserDefaults.standard
guard let pokeCatchState = defaults.bool(forKey: "pokemonCatchState") else {
print("No State available, app may be starting for first time")
return
}
if pokeCatchState {
catchLabel.setTitle("Release", for: .normal)
} else {
catchLabel.setTitle("Catch", for: .normal)
}
// Restore caught Pokemon array
if let caughtPokemonArray = defaults.value(forKey: "caughtPokemon") as [Int] {
caughtPokemon = caughtPokemonArray
}
}
Upvotes: 1