Search code examples
iosiphoneswiftstoryboardsegue

Segues and conditions for segues not working


I am working on a weather app that utilizes both core location and relies on user input to find the forecast for a specific location. The problem is the segues are not working, from the research that I have done I have also implemented a way in which I don't want the segues to go through but they don't seem to be working and I am unable to figure out why they are not working.

import UIKit
import CoreLocation
import SVProgressHUD

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager?
    var currentLocation: CLLocation?
    @IBOutlet weak var zipCodeTextField: UITextField!
    let store = CoordinatesDatastore.sharedInstance
    var userInputLocationSuccess: Bool?
    var coreLocationSuccess: Bool?

    override func viewDidLoad() {
        super.viewDidLoad()
        SVProgressHUD.setDefaultMaskType(.black)
    }

    override func viewWillAppear(_ animated: Bool) {
        self.zipCodeTextField.text = ""
        self.currentLocation = nil
        self.userInputLocationSuccess = false
        self.coreLocationSuccess = false
    }

//Core location button and I wanted to explicitly have the user press this button.

    @IBAction func getMyLocationWeatherTapped(_ sender: UIButton) {
        self.locationManager = CLLocationManager()
        self.locationManager?.delegate = self
        self.locationManager?.desiredAccuracy = kCLLocationAccuracyHundredMeters
        self.locationManager?.requestWhenInUseAuthorization()
        self.locationManager?.requestLocation()
    }

    @IBAction func goButtonTapped(_ sender: Any) {
        SVProgressHUD.show(withStatus: "Finding Your Location")
        if let userText = self.zipCodeTextField.text{
            GoogleCoordinateAPIClient.isAddressValid(zipCode: userText) { (boolValue) in
                if boolValue == true {
                    self.userInputLocationSuccess = true
                    DispatchQueue.main.async {
                        SVProgressHUD.dismiss()
                        self.presentAlert("Valid Input", message: "Found Forecast", cancelTitle: "OK")
                        self.performSegue(withIdentifier: "goButtonSegue", sender: self)
                    }
                }
                else if boolValue == false {
                    SVProgressHUD.dismiss()
                    self.userInputLocationSuccess = false
                    self.presentAlert("Invalid Input", message: "Please re-enter valid input", cancelTitle: "OK")
                }
            }
        }
    }

    func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        print("ENTERED INTO THE SEGUE FUNCTION")
         if segue.identifier == "goButtonSegue"{
            if let destinationVC = segue.destination as? WeatherForecastViewController {
                guard let neededZipcode = self.zipCodeTextField.text else {print("neededZipcode did not unwrap"); return}
                destinationVC.zipCode = neededZipcode
            }
        }
         else if segue.identifier == "coreLocationButtonSegue"{
            if let destinationVC = segue.destination as? WeatherForecastViewController {
                guard let userLocation = currentLocation else {print("did not pass user location"); return}
                destinationVC.coordinateHolder = currentLocation
            }
        }
    }

    override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        if identifier == "coreLocationButtonSegue"{
            if self.coreLocationSuccess == true && self.currentLocation != nil {
                return true
            }
        }
        else if identifier == "goButtonSegue"{
            if self.userInputLocationSuccess == true && self.zipCodeTextField.text != nil {
                return true
            }
        }
        return false
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        SVProgressHUD.show(withStatus: "Finding your location")
        if self.currentLocation == nil {
            SVProgressHUD.dismiss()
            if let personCoordinates = locations.first{
                self.currentLocation = personCoordinates
                self.coreLocationSuccess = true
                self.presentAlert("Location Found", message: "Your Location was found", cancelTitle: "OK")
                self.performSegue(withIdentifier: "coreLocationButtonSegue", sender: self)
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            self.locationManager?.startUpdatingLocation()
        }
        else if status == .notDetermined || status == .denied || status == .restricted {
            self.locationManager?.requestWhenInUseAuthorization()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        SVProgressHUD.dismiss()
        presentAlert("Location Not Found", message: "Provide zipcode, address or city", cancelTitle: "OK")
        self.shouldPerformSegue(withIdentifier: "coreLocationButton", sender: self)
    }
}

The segues are not passing over information and they aren't working as they should under the conditions specified.


Solution

  • You have the wrong method signature for your prepare(for segue:sender) function so it is not being called.

    A clue to this is that you do not have the override keyword, and the compiler is not complaining about this.

    You should have:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    

    Note the Any? vs AnyObject?