如何使用 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
}}

这是正确的做法吗?我觉得有一种更简单的方法可以简化我想要完成的工作,但我不确定。提前致谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)