【发布时间】:2014-12-08 14:40:47
【问题描述】:
我有几个 ViewControllers 都有几个方法,如下所示(这些实际上是类中唯一的方法。)
@IBAction func witnessNameAction(sender: RoundCornerButton) {
presentTextEntryFromViewController(self, initialText: incidentReport.witnessName, completion: { [unowned self] text in
self.incidentReport.witnessName = text
})
sender.setTapped()
}
@IBAction func witnessDescriptionAction(sender: RoundCornerButton) {
presentTextEntryFromViewController(self, initialText: incidentReport.witnessDescription, completion: { [unowned self] text in
self.incidentReport.witnessDescription = text
})
sender.setTapped()
}
请注意,上述两种方法之间的唯一区别是正在获取/设置哪个文本变量。我愿意接受有关如何重构这些方法的任何想法。
【问题讨论】:
-
MVVM 架构可以干掉这个。但是,由您来决定重构这是否是项目范围内的高优先级。
-
我很想听到更多@mattt。上面的代码来自一个 ViewModel。 (Apple 称它们为 ViewController,但据我所知,它们的用途相同。)
-
iOS 视图控制器不是视图模型。如果你在 iOS 上搜索 MVVM 架构,会有很多关于这个主题的好博文。
标签: swift uiviewcontroller refactoring