我想学习经典的MVP架构模式,为此尝试在Swift上实现Weather应用。我从理论上学到了它应该如何工作,但是实际上停留在基本理解上。现在我有一个模型:
模型
class WeatherModel: Codable {
var name: String?
var main: Main?
}
class Main: Codable {
var temperature: Float?
var pressure: Int?
var humidity: Int?
private enum CodingKeys: String, CodingKey {
case temperature = "temp"
case pressure
case humidity
}
查看
final class WeatherViewController: UIViewController {
@IBOutlet weak var cityTextField: UITextField!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var pressureLabel: UILabel!
@IBOutlet weak var humidityLabel: UILabel!
private var presenter: WeatherPresenter!
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
presenter = WeatherPresenter()
cityTextField.delegate = self
presenter.delegate = self
}
@IBAction func buttonClicked(_ sender: Any) {
let city = cityTextField.text
if let city = city {
presenter.loadWeatherFor(city: city)
}
}
}
extension WeatherViewController: WeatherPresenterProtocol {
// MARK: - WeatherPresenterProtocol
func showWeather(data: WeatherModel) {
if let temperature = data.main?.temperature {
self.temperatureLabel.text = String(temperature)
}
if let pressure = data.main?.pressure {
self.pressureLabel.text = Constants.pressure + String(pressure)
}
if let humidity = data.main?.humidity {
self.humidityLabel.text = Constants.humidity + String(humidity)
}
}
}
演示者
protocol WeatherPresenterProtocol: class {
func showWeather(data: WeatherModel) // ?
}
final class WeatherPresenter {
var model: WeatherModel!
weak var delegate: WeatherPresenterProtocol?
func loadWeatherFor(city: String) {
Network.shared.getWeather(city) { [weak self] (weather, error) in
DispatchQueue.main.async {
self?.model = weather
}
}
}
}
在Presenter
我接收来自网络服务的数据,但我不知道如何更新View
与此数据(如何实现协议Presenter
),因为{{1} }不应该知道的模式,但在我的情况下,它会知道(任何想法来实现经典的MVP可以理解!
和第二个问题:如何实现协议View
,以得到Presenter
(因为它是示于我已经从拍摄的图像https://www.youtube.com/watch?v=qzTeyxIW_ow)
答案 0 :(得分:1)
您可以尝试此操作,在MVP中,演示者将负责更新视图,因此请修改演示者以使其具有view属性。这里的视图只保留WeatherController实例。
就MVP而言,UIViewController子类实际上是View,而不是Presenters。
class WeatherPresenter : WeatherViewPresenter {
unowned let view: WeatherView
let weather: WeatherModel
required init(view: WeatherView, weather: WeatherModel) {
self.view = view
self.weather = weather
}
func updateWeatherView() {
//...update properties on your weather view
self.view.setTemperature(self.weather.temperature)
}
}
例如,要更新温度标签,请制定协议来设置温度值。您可以使用多种方法来设置多种属性,也可以在天气视图中为所有UI元素使用一种属性。
protocol WeatherView: class {
func setTemperature(temp: String)
//same for pressure, humid etc
}
现在为天气演示者编写一个协议
protocol WeatherViewPresenter {
init(view: WeatherView, weather: WeatherModel)
func updateWeatherView()
}
现在您已经有了天气课程的演示者设置,您将可以使用它
class WeatherViewController : UIViewController, WeatherView {
var presenter: WeatherViewPresenter!
override func viewDidLoad() {
super.viewDidLoad()
let model = //your weather model
presenter = WeatherPresenter(view: self, weather: model)
}
func setTemperature(temp: String) {
self.temperatureLabel.text = temp
}
}
从天气中获取数据后,只需调用self.presenter.updateWeatherView()
即可更新天气视图。
有关更多详细信息,请refer
答案 1 :(得分:1)
更改
func showWeather(data: WeatherModel)
到
func showWeather(temperature: String, pressure: String, humidity: String)
将这些字符串的构造从“视图”移动到“演示者”。通过这种方式,视图保持无知的模式。
演示者不应直接与完整视图对话,而只能与协议对话。