This commit is contained in:
Chris Li 2016-12-29 18:02:24 -05:00
parent 2e4891e42e
commit 243bbece05
2 changed files with 22 additions and 15 deletions

View File

@ -57,16 +57,9 @@ class RecentSearchController: UIViewController, UICollectionViewDataSource, UICo
// MARK: - CollectionView Delegate // MARK: - CollectionView Delegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let mainVC = parent?.parent?.parent as? MainController else {return} guard let cell = collectionView.cellForItem(at: indexPath) as? LocalLangCell,
print("tapped") let text = cell.label.text else {return}
Controllers.main.searchBar.searchText = text
// guard let mainVC = parent?.parent?.parent as? MainController,
// let searchController = parent?.parent as? SearchController,
// let cell = collectionView.cellForItem(at: indexPath) as? LocalLangCell,
// let text = cell.label.text else {return}
// mainVC.searchBar.searchTerm = text
// searchController.startSearch(text, delayed: false)
// collectionView.deselectItem(at: indexPath, animated: true)
} }
// MARK: - CollectionView Delegate FlowLayout // MARK: - CollectionView Delegate FlowLayout

View File

@ -17,10 +17,25 @@ class SearchBar: UIView, UITextFieldDelegate {
let delayTextChangeCallback = true let delayTextChangeCallback = true
weak var delegate: SearchBarDelegate? weak var delegate: SearchBarDelegate?
private var cachedSearchText: String? private var cachedSearchText: String?
private var previousSearchText = ""
/**
The text to display when search bar is not the first responder
*/
var title = "" { var title = "" {
didSet { didSet {
if !isFirstResponder {textField.text = title} guard !isFirstResponder else {return}
textField.text = title
}
}
/**
The search text of the search bar. It is preserved even if search bar resigns and then become first responser again
*/
var searchText = "" {
didSet {
guard searchText != textField.text && isFirstResponder else {return}
textField.text = searchText
textDidChange(textField: textField)
} }
} }
@ -112,8 +127,7 @@ class SearchBar: UIView, UITextFieldDelegate {
textField.isUserInteractionEnabled = true textField.isUserInteractionEnabled = true
textField.textAlignment = .left textField.textAlignment = .left
textField.becomeFirstResponder() textField.becomeFirstResponder()
title = textField.text ?? "" textField.text = searchText
textField.text = previousSearchText
delegate?.didBecomeFirstResponder(searchBar: self) delegate?.didBecomeFirstResponder(searchBar: self)
return true return true
} }
@ -122,7 +136,7 @@ class SearchBar: UIView, UITextFieldDelegate {
textField.isUserInteractionEnabled = false textField.isUserInteractionEnabled = false
textField.textAlignment = .center textField.textAlignment = .center
textField.resignFirstResponder() textField.resignFirstResponder()
previousSearchText = textField.text ?? "" searchText = textField.text ?? ""
textField.text = title textField.text = title
delegate?.didResignFirstResponder(searchBar: self) delegate?.didResignFirstResponder(searchBar: self)
return true return true