我正在开发一个天气应用程序,该应用程序利用核心位置并依赖用户输入来查找特定位置的预测。问题是segues不起作用,从我所做的研究中我也实现了一种方式,我不希望segues通过,但它们似乎没有工作,我无法弄清楚为什么他们不工作。
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)
}
}
segues没有传递信息,并且在指定的条件下它们不能正常工作。
答案 0 :(得分:0)
您的prepare(for segue:sender)
函数的方法签名错误,因此未被调用。
这样做的一个线索是你没有override
关键字,编译器也没有抱怨这个。
你应该:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
请注意Any?
vs AnyObject?
答案 1 :(得分:-1)
我会尝试使用故事板参考...视图控制器上的操作系统会在" storyboardID"下提供控制器ID。然后尝试以下方法:
假设您给控制器一个故事板ID为" HomeVC"
你会在删除所有的一起之后这样做:
让storyboard = UIStoryboard(名称:" Main",bundle:Bundle.main) 警卫让HomeVC = storyboard.instantiateViewController(withIdentifier:" HomeVC")为? HomeViewController else {return} present(HomeVC,animated:true,completion:nil)
这将取代"执行seque"功能