Segues 和 Segues 不工作的条件

Posted

技术标签:

【中文标题】Segues 和 Segues 不工作的条件【英文标题】:Segues and conditions for segues not working 【发布时间】:2018-10-23 14:42:51 【问题描述】:

我正在开发一个天气应用程序,该应用程序利用核心位置并依靠用户输入来查找特定位置的预报。问题是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)
    

segue 没有传递信息,也没有在指定的条件下正常工作。

【问题讨论】:

你做了什么调试?您是否在 segue 方法中设置断点以查看发生了什么? 我确实设置了断点,但它永远不会进入准备转场功能,它会转到另一个视图控制器但它没有传递信息。我确实查找了停止转场的方法,我发现执行转场功能,但从断点我可以看到它甚至不会进入为转场做准备,这是我遇到的问题。谢谢 我确实设置了断点,但它从未进入准备转场功能。我一直在寻找一种方法来停止错误处理的 segue,但它甚至不起作用,它确实继续到下一个视图控制器,但它没有传递任何信息。我似乎无法弄清楚为什么它不会传递信息。我已确保所有情节提要的标识符都已就位,并且所有内容都正确无误。 【参考方案1】:

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

一个线索是你没有override 关键字,编译器没有抱怨这个。

你应该有:

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

注意 Any?AnyObject?

【讨论】:

【参考方案2】:

我会尝试使用情节提要参考...您的视图控制器上的操作系统在“情节提要”下提供控制器 ID,然后尝试以下操作:

假设你给控制器的 storyboardID 为“HomeVC”

你会在删除所有转场后这样做:

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 守卫让 HomeVC = storyboard.instantiateViewController(withIdentifier: "HomeVC") as? HomeViewController 否则 return present(HomeVC, 动画: true, 完成: nil)

这将取代“执行序列”功能

【讨论】:

以上是关于Segues 和 Segues 不工作的条件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 CloudKit 查询条件 segues?

ios - 将一个 UIButton 连接到 2 个 segues

Segues 不能以编程方式工作

如何使用条件转场

为presentViewController设置委托(例如不使用segues)

通过 Segues,确定用户在啥视图上以及 NavigationController 按钮不出现