ios - 转场和转场条件不起作用

标签 ios iphone swift storyboard segue

我正在开发一款天气应用,它既利用核心位置,又依靠用户输入来查找特定位置的天气预报。问题是 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 没有传递信息,也没有在指定的条件下正常工作。

最佳答案

您的 prepare(for segue:sender) 函数的方法签名错误,因此它没有被调用。

这方面的一个线索是您没有 override 关键字,并且编译器不会对此报错。

你应该:

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

注意 Any?AnyObject?

关于ios - 转场和转场条件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321836/

相关文章:

ios - 带有页面 View Controller 的 removeFromSuperView

iphone - 如何在 Objective-C 中将 int 转换为 NSTimeInteravl?

ios语音识别错误域=kAFAssistantErrorDomain代码=216 "(null)"

ios - Flutter iOS 应用需要 Google 登录密码

iphone - iTunes 同步后 MPMusicPlayerController 停止工作

swift - flatMap API 合约如何将可选输入转换为非可选结果?

ios - dyld:未加载库:@rpath/libswiftCore.dylib

objective-c - UITableViewCell 未从 Storyboard 中加载 subview

ios - ScrollView 手势识别器吃掉所有的触摸事件

ios - 合并:像Future一样的发布者,但具有多个值(value)