ViewController通过segue持久化

时间:2016-08-08 13:46:46

标签: ios swift uiviewcontroller

我不知道如何解释我的问题,这里会显示一个视频,显示我遇到的问题:https://youtu.be/dfiwenMj6xc

正如你所看到的那样,推回塞回来是马虎。我想将警报标签保留在右上方,并带有正确的数据,而不是闪存。我也希望图像消失,没有可见的抖动。以下是我在第一个视图类中的代码的一部分:

class WeatherViewController: UIViewController, LocationServiceDelegate, UIViewControllerPreviewingDelegate{

    var fromWarnignsViewController = false
    var weatherAlerts: WeatherAlerts!
    var locationFixAchieved : Bool = false
    var locationStatus : NSString = "Not Started"
    var userLocation : String!
    var userLatitude : Double!
    var userLongitude : Double!
    var userTemperatureCelsius : Bool!
    var hourlyWeather2: Hourly!
    var weeklyyWeather2: Weekly!

    override func viewDidLoad() {
        super.viewDidLoad()
        }

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {        
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated);
        if (!fromWarnignsViewController){
            let currentLocation = LocationService.sharedInstance.lastLocation
            userLatitude = currentLocation?.coordinate.latitude
            userLongitude = currentLocation?.coordinate.longitude
            let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
            userTemperatureCelsius = defaults.boolForKey("celsius")
            self.warningsLabel.hidden = true
        }
    }

    func getCurrentWeatherData() -> Void {

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL(string: "\(userLocation)", relativeToURL:baseURL)
        let sharedSession = NSURLSession.sharedSession()     
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL?, response: NSURLResponse?, error: NSError?) -> Void in


            if (error == nil) {

                let dataObject = NSData(contentsOfURL: location!)
                let weatherDictionary = (try! NSJSONSerialization.JSONObjectWithData(dataObject!, options: [])) as! NSDictionary

                let currentWeather = Current(weatherDictionary: weatherDictionary)
                let weeklyWeather = Weekly(weatherDictionary: weatherDictionary)
                self.hourlyWeather2 = Hourly(weatherDictionary: weatherDictionary)
                self.weeklyyWeather2 = weeklyWeather

                let hourlyWeather = self.hourlyWeather2
                dispatch_async(dispatch_get_main_queue(), {
                    self.weatherAlerts = WeatherAlerts(weatherDictionary: weatherDictionary)
                    if(self.weatherAlerts.userAlert.count > 0){
                        self.warningsLabel.hidden = false;
                        let buttonTitle = String(self.weatherAlerts.userAlert.count) + " Alerts"

                        self.warningsLabel.setTitle(buttonTitle, forState: UIControlState.Normal)
                        self.warningsLabel.enabled = true

                    } else {
                        self.warningsLabel.hidden = true;
                        let buttonTitle = "0 Alerts"
                        self.warningsLabel.setTitle(buttonTitle, forState: UIControlState.Normal)
                        self.warningsLabel.enabled = false
                    }
                });

            }
        })

        downloadTask.resume()
    }    

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if (segue.identifier == "warnings") {
            let svc = segue.destinationViewController as! WarningsViewController;

            svc.weatherAlerts = self.weatherAlerts
            svc.userLocation = self.userLocation
            svc.userLatitude = self.userLatitude
            svc.userLongitude = self.userLongitude
            svc.hourlyWeather2 = self.hourlyWeather2
            svc.weeklyyWeather2 = self.weeklyyWeather2
            svc.weatherViewController = self
        }
    }

    func tracingLocation(currentLocation: CLLocation) {
        // IF APPLICATION IS ACTIVE
        if UIApplication.sharedApplication().applicationState == .Active {
            self.userLatitude = currentLocation.coordinate.latitude;
            self.userLongitude = currentLocation.coordinate.longitude;
            activeLocationUpdate(currentLocation)
            getCurrentWeatherData()
        }
    }
}

以下是警报视图中的代码:

class WarningsViewController: UIViewController {
    var weatherViewController: WeatherViewController!
    var weatherAlerts: WeatherAlerts!
    var userLocation : String!
    var userLatitude : Double!
    var userLongitude : Double!
    var hourlyWeather2: Hourly!
    var weeklyyWeather2: Weekly!

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        print(weatherAlerts.userAlert)

        if (segue.identifier == "embed") {
            let svc = segue.destinationViewController as! WarningTableViewController;

            svc.weatherAlerts = self.weatherAlerts

        } else if (segue.identifier == "back") {
            let svc = segue.destinationViewController as! WeatherViewController;
            svc.weatherAlerts = self.weatherAlerts
            svc.userLocation = self.userLocation
            svc.userLatitude = self.userLatitude
            svc.userLongitude = self.userLongitude
            svc.hourlyWeather2 = self.hourlyWeather2
            svc.weeklyyWeather2 = self.weeklyyWeather2
            svc.fromWarnignsViewController = true
        }  
    }
}

这是很多代码,但我不知道如何压缩。任何帮助都会非常感激!

0 个答案:

没有答案