iOS development Swift-11-forward value transfer, search, reverse value transfer, city id to get weather, cursor focus, intercepting blank/space characters-Hefeng Weather App secondary page code

1.Create the controller class of the next page
Insert image description hereInsert image description here

Select the secondary interface in Main, click the yellow circle in the upper left corner, and change the class in its Custom Class to QueryViewController.
Insert image description here

Copy the automatically generated homepage value-passing method in QueryViewController to ViewController. Remove the comment symbols.
Insert image description here

2. In Main, name the arrow that transfers values ​​from page 1 to page 2 QueryViewControllerSegue.
Insert image description here

3. Code to forward the city value from the home page to the next page.

ViewController:

import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate {    //遵循CLLocationManagerDelegate协议
    @IBOutlet weak var tempLable: UILabel!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    
    //位置管理器
    let locationManager = CLLocationManager()
    //Weather.swift实例化
    let weather = Weather()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                //print(weatherJSON["now"]["temp"])    //或weatherJSON["now", "temp"]
                //print(weatherJSON["refer"]["sources"][0])     //weatherJSON["refer", "sources", 0]
                
                //MVC结构
                self.weather.temp = "\(weatherJSON["now"]["temp"].stringValue)˚"
                self.weather.icon = weatherJSON["now"]["icon"].stringValue
                
                self.tempLable.text = self.weather.temp
                self.iconImageView.image = UIImage(named: self.weather.icon)
            }
        }      //请求和风API的网址,得到当前位置的天气
        
        AF.request("https://geoapi.qweather.com/v2/city/lookup?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city
            }
        }
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        cityLable.text = "获取用户城市失败"
    }
    
    // Navigation跳转
    // 跳转前到准备工作,从当前页面经过的所有跳转都会经过这个方法
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// 方法1.
        //if segue.identifier == "QueryViewControllerSegue"{
        //    let viewController = segue.destination as! QueryViewController
        //    viewController.currentCity = weather.city
        //}
// 方法2.

        if let viewController = segue.destination as? QueryViewController{

            ViewController.currentCity = weather.city

    }

}

QueryViewController:

import UIKit

class QueryViewController: UIViewController {
    
    var currentCity = "双流"

    @IBOutlet weak var currentCityLable: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //从首页将城市名字传到次页(正向传值)
        currentCityLable.text = currentCity    
    }
    
}

4. Start the test and the forward value transfer is successful.
Insert image description hereInsert image description here

5. Pass the value in the reverse direction and pass the value obtained in the input box on the next page to the weather on the home page. Create a return button and a search button.

QueryViewController:

import UIKit

protocol QueryViewControllerDelegate {
    func didChangeCity(city: String)
}

class QueryViewController: UIViewController {
    
    var currentCity = ""
    var delegate: QueryViewControllerDelegate?

    @IBOutlet weak var currentCityLable: UILabel!
    @IBOutlet weak var cityTextfield: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //从首页将城市名字传到次页(正向传值)
        currentCityLable.text = currentCity
    }
    //返回按钮
    @IBAction func back(_ sender: Any) {
        dismiss(animated: true)
    }
    //查询按钮
    @IBAction func query(_ sender: Any) {
        dismiss(animated: true)
        
        delegate?.didChangeCity(city: cityTextfield.text!)
    }
}

ViewController:

import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate, QueryViewControllerDelegate {    //遵循CLLocationManagerDelegate协议
    @IBOutlet weak var tempLable: UILabel!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    
    //位置管理器
    let locationManager = CLLocationManager()
    //Weather.swift实例化
    let weather = Weather()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                //print(weatherJSON["now"]["temp"])    //或weatherJSON["now", "temp"]
                //print(weatherJSON["refer"]["sources"][0])     //weatherJSON["refer", "sources", 0]
                
                //MVC结构
                self.weather.temp = "\(weatherJSON["now"]["temp"].stringValue)˚"
                self.weather.icon = weatherJSON["now"]["icon"].stringValue
                
                self.tempLable.text = self.weather.temp
                self.iconImageView.image = UIImage(named: self.weather.icon)
                
                
            }
        }      //请求和风API的网址,得到当前位置的天气
        
        AF.request("https://geoapi.qweather.com/v2/city/lookup?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city
            }
        }
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        cityLable.text = "获取用户城市失败"
    }
    
    // Navigation跳转
    
    // 跳转前到准备工作,从当前页面经过的所有跳转都会经过这个方法
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        //        if segue.identifier == "QueryViewControllerSegue"{
        //            let viewController = segue.destination as! QueryViewController
        //            viewController.currentCity = weather.city
        //        }
        
        if let viewController = segue.destination as? QueryViewController{
            viewController.currentCity = weather.city
            viewController.delegate = self
        }
        
        
    }
    func didChangeCity(city: String) {
        print(city)
    }
}

6. Obtain the city name that the user wants to query through search

ViewController:

import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate, QueryViewControllerDelegate {    //遵循CLLocationManagerDelegate协议
    @IBOutlet weak var tempLable: UILabel!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    
    //位置管理器
    let locationManager = CLLocationManager()
    //Weather.swift实例化
    let weather = Weather()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                //print(weatherJSON["now"]["temp"])    //或weatherJSON["now", "temp"]
                //print(weatherJSON["refer"]["sources"][0])     //weatherJSON["refer", "sources", 0]
                
                //MVC结构
                self.weather.temp = "\(weatherJSON["now"]["temp"].stringValue)˚"
                self.weather.icon = weatherJSON["now"]["icon"].stringValue
                
                self.tempLable.text = self.weather.temp
                self.iconImageView.image = UIImage(named: self.weather.icon)
                
                
            }
        }      //请求和风API的网址,得到当前位置的天气
        
        AF.request("https://geoapi.qweather.com/v2/city/lookup?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city
            }
        }
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        cityLable.text = "获取用户城市失败"
    }
    
    // Navigation跳转
    
    // 跳转前到准备工作,从当前页面经过的所有跳转都会经过这个方法
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        //        if segue.identifier == "QueryViewControllerSegue"{
        //            let viewController = segue.destination as! QueryViewController
        //            viewController.currentCity = weather.city
        //        }
        
        if let viewController = segue.destination as? QueryViewController{
            viewController.currentCity = weather.city
            viewController.delegate = self
        }
        
        
    }
    func didChangeCity(city: String) {
        //print(city)
        let parameters = ["location": city, "key": "a91848aaab484a3599a703b139dfe87b"]
        AF.request("https://geoapi.qweather.com/v2/city/lookup", parameters: parameters).responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city
            }
        }
        
    }
}

7. Optimize to obtain the current weather directly through the city ID to avoid being troubled by cities with the same name.

ViewController:

import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate, QueryViewControllerDelegate {    //遵循CLLocationManagerDelegate协议
    @IBOutlet weak var tempLable: UILabel!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    
    //位置管理器
    let locationManager = CLLocationManager()
    //Weather.swift实例化
    let weather = Weather()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                //print(weatherJSON["now"]["temp"])    //或weatherJSON["now", "temp"]
                //print(weatherJSON["refer"]["sources"][0])     //weatherJSON["refer", "sources", 0]
                
                //MVC结构
                self.weather.temp = "\(weatherJSON["now"]["temp"].stringValue)˚"
                self.weather.icon = weatherJSON["now"]["icon"].stringValue
                
                self.tempLable.text = self.weather.temp
                self.iconImageView.image = UIImage(named: self.weather.icon)
                
                
            }
        }      //请求和风API的网址,得到当前位置的天气
        
        AF.request("https://geoapi.qweather.com/v2/city/lookup?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city
            }
        }
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        cityLable.text = "获取用户城市失败"
    }
    
    // Navigation跳转
    
    // 跳转前到准备工作,从当前页面经过的所有跳转都会经过这个方法
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        //        if segue.identifier == "QueryViewControllerSegue"{
        //            let viewController = segue.destination as! QueryViewController
        //            viewController.currentCity = weather.city
        //        }
        
        if let viewController = segue.destination as? QueryViewController{
            viewController.currentCity = weather.city
            viewController.delegate = self
        }
        
        
    }
    func didChangeCity(city: String) {
        //print(city)
        let parameters = ["location": city, "key": "a91848aaab484a3599a703b139dfe87b"]
        AF.request("https://geoapi.qweather.com/v2/city/lookup", parameters: parameters).responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city

                //通过城市id查询当前城市天气

                let parameters = ["location": cityJSON["location", 0, "id"].stringValue, "key": "a91848aaab484a3599a703b139dfe87b"]

                AF.request("https://devapi.qweather.com/v7/weather/now", parameters: parameters).responseJSON { response in

                    if let data = response.value{

                        let weatherJSON = JSON(data)

                        //处理数据

                        self.weather.temp = "\(weatherJSON["now","temp"].stringValue)°"

                        self.weather.icon = weatherJSON["now","icon"].stringValue

                        //处理UI

                        self.tempLable.text = self.weather.temp

                        self.iconImageView.image = UIImage(named: self.weather.icon)

                    }

                }

           }

        }
        
    }
}

8. Optimize, focus the cursor directly on the search bar on the next page to reduce the user's click operations.

QueryViewController:

import UIKit

protocol QueryViewControllerDelegate {
    func didChangeCity(city: String)
}

class QueryViewController: UIViewController {
    
    var currentCity = ""
    var delegate: QueryViewControllerDelegate?

    @IBOutlet weak var currentCityLable: UILabel!
    @IBOutlet weak var cityTextfield: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //将光标聚焦到搜索栏处
        cityTextfield.becomeFirstResponder()  //收起键盘:cityTextfield.resignFirstResponder()
        
        //从首页将城市名字传到次页(正向传值)
        currentCityLable.text = currentCity
    }
    //返回按钮
    @IBAction func back(_ sender: Any) {
        dismiss(animated: true)
    }
    //查询按钮
    @IBAction func query(_ sender: Any) {
        dismiss(animated: true)
        
        delegate?.didChangeCity(city: cityTextfield.text!)
    }
 
}

9. Intercept users' blank searches and reduce resource consumption.

QueryViewController:

import UIKit

protocol QueryViewControllerDelegate {
    func didChangeCity(city: String)
}

class QueryViewController: UIViewController {
    
    var currentCity = ""
    var delegate: QueryViewControllerDelegate?

    @IBOutlet weak var currentCityLable: UILabel!
    @IBOutlet weak var cityTextfield: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //将光标聚焦到搜索栏处
        cityTextfield.becomeFirstResponder()  //收起键盘:cityTextfield.resignFirstResponder()
        
        //从首页将城市名字传到次页(正向传值)
        currentCityLable.text = currentCity
    }
    //返回按钮
    @IBAction func back(_ sender: Any) {
        dismiss(animated: true)
    }
    //查询按钮
    @IBAction func query(_ sender: Any) {
        dismiss(animated: true)
        if !cityTextfield.text!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty{//修剪掉用户输入的空格和回车,如果修剪掉之后字符串 不(!) 为空,则进行搜索
            delegate?.didChangeCity(city: cityTextfield.text!)
        }
    }
 
} 

10. Start testing:
Insert image description hereInsert image description hereInsert image description hereInsert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/LYly_B/article/details/132800412