如何使用 MVC 正确清理我的代码?
How to properly clean up my code using MVC?
基本上,我在 TableViewController 中有以下代码,除了一些额外的行外,它基本上在另一个 CollectionViewController 中重复:
func configureSearchController() {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Albums"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.delegate = self
}
为了清理我两个控制器中的代码,我想将这些方法移动到一个新文件中,如下所示:
class SearchBarManager: UIViewController {
func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Albums"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
}
func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Photos"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["1 Column", "2 Columns", "3 Columns"]
searchController.searchBar.selectedScopeButtonIndex = 2
}}
这是正确的做法吗?我觉得有一种更简单的方法可以简化我要完成的工作,但我不确定。提前致谢!
保持代码整洁非常重要,它使您能够重用代码。要实现这一点,您可以使用一些常见的设计模式,如策略,您可以使用面向协议的编程。
我将留下一些资源,以便您可以更深入地了解如何使用 SOLID 原则实现这一目标:https://medium.com/ios-expert-series-or-interview-series/solid-design-principle-using-swift-34bb1731cfb3
在这种情况下您需要的是一个简单的抽象,您需要创建一个能够配置任何 SearchController 的函数。我会给你一个例子,但不要让这个例子限制你,还有很多其他(和更好的方法)可以实现这一点。
class SearchBarManager: UIViewController {
private func configureSearchController(searchController: UISearchController, navigationItem: UINavigationItem, title: String, scopeButtonTitles: [String] = [], selectedScopeButtonIndex: Int = 0) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = title
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = scopeButtonTitles
searchController.searchBar.selectedScopeButtonIndex = selectedScopeButtonIndex
}
func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Albums")
}
func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Photos", scopeButtonTitles: ["1 Column", "2 Columns", "3 Columns"], selectedScopeButtonIndex: 2)
}
}
基本上,我在 TableViewController 中有以下代码,除了一些额外的行外,它基本上在另一个 CollectionViewController 中重复:
func configureSearchController() {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Albums"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.delegate = self
}
为了清理我两个控制器中的代码,我想将这些方法移动到一个新文件中,如下所示:
class SearchBarManager: UIViewController {
func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Albums"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
}
func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Photos"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["1 Column", "2 Columns", "3 Columns"]
searchController.searchBar.selectedScopeButtonIndex = 2
}}
这是正确的做法吗?我觉得有一种更简单的方法可以简化我要完成的工作,但我不确定。提前致谢!
保持代码整洁非常重要,它使您能够重用代码。要实现这一点,您可以使用一些常见的设计模式,如策略,您可以使用面向协议的编程。
我将留下一些资源,以便您可以更深入地了解如何使用 SOLID 原则实现这一目标:https://medium.com/ios-expert-series-or-interview-series/solid-design-principle-using-swift-34bb1731cfb3
在这种情况下您需要的是一个简单的抽象,您需要创建一个能够配置任何 SearchController 的函数。我会给你一个例子,但不要让这个例子限制你,还有很多其他(和更好的方法)可以实现这一点。
class SearchBarManager: UIViewController {
private func configureSearchController(searchController: UISearchController, navigationItem: UINavigationItem, title: String, scopeButtonTitles: [String] = [], selectedScopeButtonIndex: Int = 0) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = title
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = scopeButtonTitles
searchController.searchBar.selectedScopeButtonIndex = selectedScopeButtonIndex
}
func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Albums")
}
func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Photos", scopeButtonTitles: ["1 Column", "2 Columns", "3 Columns"], selectedScopeButtonIndex: 2)
}
}