其实MVVM和Presentation Controls差不多就是一个概念,MVVM在网上随便一搜就有很多教程。
接下来用一个例子来解说一下:
1.创建Trip类
class Trip {
//属性
let departure: NSDate
let arrival: NSDate
let actualDeparture: NSDate
let delay: NSTimeInterval
let delayed: Bool
let duration: NSTimeInterval
//初始化方法
init(departure: NSDate, arrival: NSDate, actualDeparture: NSDate? = nil) {
self.departure = departure
self.arrival = arrival
self.actualDeparture = actualDeparture ?? departure
// 计算
duration = self.arrival.timeIntervalSinceDate(self.departure)
delay = self.actualDeparture.timeIntervalSinceDate(self.departure)
delayed = delay > 0
}
}
2.创建TripViewModel类
class TripViewModel {
let date: String
let departure: String
let arrival: String
let duration: String
let delay: String?
let delayHidden: Bool
let departureTimeColor: UIColor
init(_ trip: Trip) {
date = NSDateFormatter.localizedStringFromDate(trip.departure, dateStyle: .ShortStyle, timeStyle: .NoStyle)
departure = NSDateFormatter.localizedStringFromDate(trip.departure, dateStyle: .NoStyle, timeStyle: .ShortStyle)
arrival = NSDateFormatter.localizedStringFromDate(trip.arrival, dateStyle: .NoStyle, timeStyle: .ShortStyle)
let durationFormatter = NSDateComponentsFormatter()
durationFormatter.allowedUnits = [.Hour, .Minute]
durationFormatter.unitsStyle = .Short
duration = durationFormatter.stringFromTimeInterval(trip.duration)!
delayHidden = !trip.delayed
if trip.delayed {
durationFormatter.unitsStyle = .Full
delay = String.localizedStringWithFormat(NSLocalizedString("%@ delay", comment: "Show the delay"), durationFormatter.stringFromTimeInterval(trip.delay)!)
departureTimeColor = .redColor()
} else {
self.delay = nil
departureTimeColor = UIColor(red: 0, green: 0, blue: 0.4, alpha: 1)
}
}
}
3.在viewcontrollers中使用viewmodel
class ViewController: UIViewController {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var departureTimeLabel: UILabel!
@IBOutlet weak var arrivalTimeLabel: UILabel!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var delayLabel: UILabel!
//初始化viewmodel
let tripModel = TripViewViewModel(Trip(departure: NSDate(timeIntervalSince1970: 1444396193), arrival: NSDate(timeIntervalSince1970: 1444397193), actualDeparture: NSDate(timeIntervalSince1970: 1444396493)))
override func viewDidLoad() {
super.viewDidLoad()
dateLabel.text = tripModel.date
departureTimeLabel.text = tripModel.departure
arrivalTimeLabel.text = tripModel.arrival
durationLabel.text = tripModel.duration
delayLabel.text = tripModel.delay
delayLabel.hidden = tripModel.delayHidden
departureTimeLabel.textColor = tripModel.departureTimeColor
}
}
用一个常量属性来代替设置viewmodel,会通过prepareForSegue方法传递给控制器,这里已经是一个比较好的视图控制器了,因为这里不包含任何数据传递逻辑给viewmodel。
4.使用Presentation controls代替viewmodel
class TripPresentationControl: NSObject {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var departureTimeLabel: UILabel!
@IBOutlet weak var arrivalTimeLabel: UILabel!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var delayLabel: UILabel!
var trip: Trip! {
didSet {
dateLabel.text = NSDateFormatter.localizedStringFromDate(trip.departure, dateStyle: .ShortStyle, timeStyle: .NoStyle)
departureTimeLabel.text = NSDateFormatter.localizedStringFromDate(trip.departure, dateStyle: .NoStyle, timeStyle: .ShortStyle)
arrivalTimeLabel.text = NSDateFormatter.localizedStringFromDate(trip.arrival, dateStyle: .NoStyle, timeStyle: .ShortStyle)
let durationFormatter = NSDateComponentsFormatter()
durationFormatter.allowedUnits = [.Hour, .Minute]
durationFormatter.unitsStyle = .Short
durationLabel.text = durationFormatter.stringFromTimeInterval(trip.duration)!
delayLabel.hidden = !trip.delayed
if trip.delayed {
durationFormatter.unitsStyle = .Full
delayLabel.text = String.localizedStringWithFormat(NSLocalizedString("%@ delay", comment: "Show the delay"), durationFormatter.stringFromTimeInterval(trip.delay)!)
departureTimeLabel.textColor = .redColor()
}
}
}
}
class ViewController: UIViewController {
@IBOutlet var tripPresentationControl: TripPresentationControl!
let trip = Trip(departure: NSDate(timeIntervalSince1970: 1444396193), arrival: NSDate(timeIntervalSince1970: 1444397193), actualDeparture: NSDate(timeIntervalSince1970: 1444396493))
override func viewDidLoad() {
super.viewDidLoad()
tripPresentationControl.trip = trip
}
}
是不是这样看上去更清楚了。
5.优化一下
// 使用结构体
struct TripViewModel {
let date: String
let departure: String
let arrival: String
let duration: String
let delay: String?
let delayHidden: Bool
let departureTimeColor: UIColor
init(date: String, departure: String, arrival: String,
duration: String, delay: String?, delayHidden: Bool,
departureTimeColor: UIColor) {
self.date = date
self.departure = departure
self.arrival = arrival
self.duration = duration
self.delay = delay
self.delayHidden = delayHidden
self.departureTimeColor = departureTimeColor
}
}
class TripPresentationControl: NSObject {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var departureTimeLabel: UILabel!
@IBOutlet weak var arrivalTimeLabel: UILabel!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var delayLabel: UILabel!
var tripViewModel: TripViewModel! {
didSet {
dateLabel.text = tripViewModel.date
departureTimeLabel.text = tripViewModel.departure
arrivalTimeLabel.text = tripViewModel.arrival
durationLabel.text = tripViewModel.duration
let hideDelay = tripViewModel.delayHidden
delayLabel.hidden = hideDelay
if let delayText = tripViewModel.delay where !hideDelay {
durationFormatter.unitsStyle = .Full
delayLabel.text = delayText
departureTimeLabel.textColor = tripViewModel.departureTimeColor
}
}
}
}
class ViewController: UIViewController {
@IBOutlet var tripPresentationControl: TripPresentationControl!
var tripViewModel: TripViewModel!
override func viewDidLoad() {
super.viewDidLoad()
tripPresentationControl.tripViewModel = tripViewModel
}
}
//进行扩展
extension TripViewModel {
init(_ trip: Trip) {
date = NSDateFormatter.localizedStringFromDate(trip.departure, dateStyle: .ShortStyle, timeStyle: .NoStyle)
departure = NSDateFormatter.localizedStringFromDate(trip.departure, dateStyle: .NoStyle, timeStyle: .ShortStyle)
arrival = NSDateFormatter.localizedStringFromDate(trip.arrival, dateStyle: .NoStyle, timeStyle: .ShortStyle)
let durationFormatter = NSDateComponentsFormatter()
durationFormatter.allowedUnits = [.Hour, .Minute]
durationFormatter.unitsStyle = .Short
duration = durationFormatter.stringFromTimeInterval(trip.duration)!
delayHidden = !trip.delayed
if trip.delayed {
durationFormatter.unitsStyle = .Full
delay = String.localizedStringWithFormat(NSLocalizedString("%@ delay", comment: "Show the delay"), durationFormatter.stringFromTimeInterval(trip.delay)!)
departureTimeColor = .redColor()
} else {
self.delay = nil
departureTimeColor = UIColor(red: 0, green: 0, blue: 0.4, alpha: 1)
}
}
}
class ShowExistingTrip {
let view: ViewController
init(view: ViewController) {
self.view = view
}
看客肯定臭骂偶一顿,卧槽,全是code。