微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift 2.0 下面向协议的MVVM架构实践

本文由CocoaChina译者lynulzy(社区ID,博客)翻译
原文:Swift 2.0: Protocol-Oriented MVVM


自从令人兴奋的[ 《面向协议的编程方法》 ]在Swift的WWDC大会上发布以来。我对协议的使用考虑了很多。但是在现实中,我并没有太多的顾及和使用这些功能。我还仍旧在消化到底面向协议的编程方法是什么,在代码的哪些地方应该使用,而不是使用我目前使用的`go-to`编程方法


...所以,当我想起来要在哪里应用这些概念性的东西时,我非常激动,那就是MVVM !我已经在之前的博客中使用过MVVM架构,如果你想了解更多MVVM相关知识请参考[这里]。接下来我将讲解,如何添加面向协议。

我将会使用一个简单的例子。一个只有一个设置选项的设置页面,把应用设置为Minion模式,当然你也可以扩展为多个设置选项。

View Cell

一个极其普通的Cell,它包含一个Label和一个开关控件。你也可以在其他地方使用这个Cell,例如注册页面添加一个“记住我”的开关选项。所以,你应该保持这个页面通用性。

一个复杂的配置

通常,我在cell中使用一个设置方法,来监听所有对应用设置可能的变更,这看起来是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
classSwitchWithTextTableViewCell:UITableViewCell{
@IBOutletprivateweak var label:UILabel!
switchToggle:UISwitch!
typealiasonSwitchToggleHandlerType=(switchOn:Bool)->Void
private onSwitchToggleHandler:onSwitchToggleHandlerType?
overridefuncawakeFromNib(){
super .awakeFromNib()
}
funcconfigure(withTitletitle:String,
switchOn:Bool,
onSwitchToggleHandler:onSwitchToggleHandlerType?=nil)
{
label.text=title
switchToggle.on=switchOn
self.onSwitchToggleHandler=onSwitchToggleHandler
}
@IBActionfunconSwitchToggle(sender:UISwitch){
onSwitchToggleHandler?(switchOn:sender.on)
}
}

通过 Swift 的认参数,可以添加其他的设置选项到这个设置方法,而不必改变代码中的其他地方,使用起来非常方便。例如,当设计师说开关按钮的颜色需应该各不相同,这时候我就可以添加一个认参数。

12
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
switchColor:UIColor=.purpleColor(),
onSwitchToggleHandler:onSwitchToggleHandlerType?=nil)
{
label.text=title
switchToggle.on=switchOn
//coloroptionadded!
switchToggle.onTintColor=switchColor
self.onSwitchToggleHandler=onSwitchToggleHandler
虽然在这种情况下看起来并不是什么大问题,但是随着时间的增加,事实上这个方法将会变得非常冗长、复杂!是时候由面向协议的编程方法登场了。

面向协议的编程方法

26
27
28
29
protocolSwitchWithTextCellProtocol{
title:String{get}
switchOn:Bool{get}
funconSwitchTogleOn(on:Bool)
}
classSwitchWithTextTableViewCell:UITableViewCell{
label:UILabel!
switchToggle:UISwitch!
delegate:SwitchWithTextCellProtocol?
overridefuncawakeFromNib(){
.awakeFromNib()
}
funcconfigure(withDelegatedelegate:SwitchWithTextCellProtocol){
self.delegate=delegate
label.text=delegate.title
switchToggle.on=delegate.switchOn
}
@IBActionfunconSwitchToggle(sender:UISwitch){
delegate?.onSwitchTogleOn(sender.on)
}
当设计师说需要改变开关控件颜色的时候会发生什么?以下代码可以展现协议扩展的奇妙之处。

21
extensionSwitchWithTextCellProtocol{
//setthedefaultcolorhere!
funcswitchColor()->UIColor{
return .purpleColor()
}
}
classSwitchWithTextTableViewCell:UITableViewCell{
//truncated,seeabove
funcconfigure(withDelegatedelegate:SwitchWithTextCellProtocol){
self.delegate=delegate
label.text=delegate.title
switchToggle.on=delegate.switchOn
//coloroptionadded!
switchToggle.onTintColor=delegate.switchColor()
}
在以上代码中协议的扩展实现了认的switchColor选项,所以,任何已经实现了这个协议或者并不关心设置开关颜色的人,不用关注这个扩展。只有一个具有不同颜色的新的开关控件可以实现。

viewmodel

所以现在剩下的事情将会非常简单。我将会为MinionMode的设置cell写一个viewmodel。

18
importUIKit
structMinionModeviewmodel:SwitchWithTextCellProtocol{
title= "MinionMode!!!"
switchOn= true
funconSwitchTogleOn(on:Bool){
if on{
print( "TheMinionsareheretostay!" )
} else {
"TheMinionswentouttoplay!" )
}
}
funcswitchColor()->UIColor{
.yellowColor()
}
ViewController

最后一步就是在ViewController中设置cell的时候将viewmodel传给cell。

29
30
31
32
33
34
35
36
37
38
39
classSettingsViewController:UITableViewController{
enumSetting:Int{
case MinionMode
//othersettingshere
}
overridefuncviewDidLoad(){
.viewDidLoad()
//MARK:-Tableviewdatasource
overridefunctableView(tableView:UITableView,
numberOfRowsInSectionsection:Int)->Int
{
1
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell
{
letsetting=Setting(rawValue:indexPath.row){
switch setting{
.MinionMode:
letcell=tableView.dequeueReusableCellWithIdentifier( "SwitchWithTextTableViewCell" ,forIndexPath:indexPath)as!SwitchWithTextTableViewCell
//thisiswherethemagichappens!
cell.configure(withDelegate:MinionModeviewmodel())
cell
}
}
tableView.dequeueReusableCellWithIdentifier( "defaultCell" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forIndexPath:indexPath)
}
通过使用协议的扩展,是面向协议的编程方法有了很大的意义,并且我在寻找更多的使用场景。以上代码的全部内容放在[github]上。

更新:将数据源和代理分开

评论中,marc Baldwin 建议分开cell的数据源和代理方法到两个协议中,就像UITableView中的那样。我很赞成这个意见,以下是我修改后的代码

View Cell

Cell将拥有两个协议,并且任何一个协议都可以设置这个cell。

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
protocolSwitchWithTextCellDataSource{
title:String{get}
switchOn:Bool{get}
protocolSwitchWithTextCellDelegate{
funconSwitchTogleOn(on:Bool)
switchColor:UIColor{get}
textColor:UIColor{get}
font:UIFont{get}
}
extensionSwitchWithTextCellDelegate{
switchColor:UIColor{
.purpleColor()
textColor:UIColor{
.blackColor()
font:UIFont{
.systemFontOfSize(17)
}
classSwitchWithTextTableViewCell:UITableViewCell{
label:UILabel!
switchToggle:UISwitch!
dataSource:SwitchWithTextCellDataSource?
delegate:SwitchWithTextCellDelegate?
overridefuncawakeFromNib(){
.awakeFromNib()
}
funcconfigure(withDataSourcedataSource:SwitchWithTextCellDataSource,delegate:SwitchWithTextCellDelegate?){
self.dataSource=dataSource
self.delegate=delegate
label.text=dataSource.title
switchToggle.on=dataSource.switchOn
//coloroptionadded!
switchToggle.onTintColor=delegate?.switchColor
}
@IBActionfunconSwitchToggle(sender:UISwitch){
delegate?.onSwitchTogleOn(sender.on)
}
viewmodel

你现在可以在扩展里把数据源和delegate逻辑分开了:

structMinionModeviewmodel:SwitchWithTextCellDataSource{
true
extensionMinionModeviewmodel:SwitchWithTextCellDelegate{
funconSwitchTogleOn(on:Bool){
on{
)
{
)
}
}
.yellowColor()
ViewController

这一部分是我不十分确定,ViewController不能传递viewmodel两次:

17
cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell
{
letsetting=Setting(rawValue:indexPath.row){
setting{
.MinionMode:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forIndexPath:indexPath)as!SwitchWithTextTableViewCell
//thisiswherethemagichappens!
letviewmodel=MinionModeviewmodel()
cell.configure(withDataSource:viewmodel,delegate:viewmodel)
cell
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forIndexPath:indexPath)
代码已经上传[GitHub]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐