user6886286
user6886286

Reputation:

Transferring Data Between Views, Saving in UserDefaults

I am having a problem in my app where my label (goalLabel) won't get changed to the goal let (which is set in UserDefaults) on the app open. It will get changed to null for some reason. The code below should check if the data that gets trasnmitted from another view controller (Where the goal is set) is equal to null. if it isnt itll change it to whatever the goalDataPassed is but if it is equal to null (Which it should be on app open) it should change it to whatever is saved in userdefaults.

SecondViewController (Where the label should appear):

@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var jobLabel: UILabel!
@IBOutlet weak var goalLabel: UILabel!

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()
var firstDataPassed:String!
var secondDataPassed:String!
var goalDataPassed:String!


let defaults = UserDefaults.standard
let name = UserDefaults.standard.string(forKey: "name")
let job = UserDefaults.standard.string(forKey: "job")
let goal = UserDefaults.standard.string(forKey: "goal")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view

        nameLabel.text = name
        jobLabel.text = job
        goalLabel.text = goal

    if (firstDataPassed != "") {
        nameLabel.text = firstDataPassed
    }
    if (secondDataPassed != "") {
        jobLabel.text = secondDataPassed
    }
    if (goalDataPassed != "") {
        goalLabel.text = goalDataPassed
    }

    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

    let initialLocation = CLLocation(latitude: -34.9285, longitude: 138.6007)

    let regionRadius: CLLocationDistance = 20000
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius, regionRadius)
        mapView.setRegion(coordinateRegion, animated: true)
    }
    centerMapOnLocation(location: initialLocation)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last
    let viewRegion = MKCoordinateRegionMakeWithDistance((userLocation?.coordinate)!, 10000, 10000)
    self.mapView.setRegion(viewRegion, animated: true)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

Upvotes: 0

Views: 66

Answers (1)

Fabian
Fabian

Reputation: 5348

As an example: You can make firstDataPassed an optional and then do the assignment the following way:

nameLabel.text = firstDataPassed ?? name!

If firstDataPassed is nil, name! will be used for the text. And if name! crashes you know that the default value is bad.

You could also check for firstDataPassed to be the empty string, but if the other viewController sets firstDataPassed if it should be used then that is not necessary.

Upvotes: 1

Related Questions