Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

swift - How to properly clean up my code using MVC?

Basically, I have the following code in a TableViewController which is basically repeated in another CollectionViewController except for a few extra lines:

  func configureSearchController() {
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Albums"
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    definesPresentationContext = true
    searchController.searchBar.delegate = self
  }

In order to clean up the code in both my controllers, I wanted to move these methods to a new file, like so:

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
}}

Is this the proper way to do it? I feel like there is a simpler way to simplify what I'm trying to accomplish, but I'm not sure. Thanks in advance!

question from:https://stackoverflow.com/questions/65847929/how-to-properly-clean-up-my-code-using-mvc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Keep your code clean is very important, it enables you to reuse code. To achieve this you could use some common design patterns like Strategy and you could use protocol-oriented programming.

I will leave some sources so you can go deeper into how to achieve this by using SOLID principles: https://medium.com/ios-expert-series-or-interview-series/solid-design-principle-using-swift-34bb1731cfb3

What you need in this case is a simple abstraction, you need to create a function that is able to configure any SearchController. I will give you an example, but don't let this example to limit you, there are many other (and better ways) to achieve this.

 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)
}

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...