From e9bda7d65db95edd1e0a301accf2884db1ab12c2 Mon Sep 17 00:00:00 2001 From: Will Hains Date: Sun, 7 Dec 2014 16:46:31 +0900 Subject: [PATCH] Fix for issue #7: Fails to search on autocorrect. --- .../Dictionary/DictionaryViewController.swift | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/code/Dictionary/DictionaryViewController.swift b/code/Dictionary/DictionaryViewController.swift index 1beebb2..7ffd647 100644 --- a/code/Dictionary/DictionaryViewController.swift +++ b/code/Dictionary/DictionaryViewController.swift @@ -33,37 +33,47 @@ class DictionaryViewController: UIViewController, UISearchBarDelegate self.searchBar.becomeFirstResponder() } + func searchDictionary() + { + // Check if dictionary contains typed word + let searchText = searchBar.text + if UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm(searchText) + { + // Remove the existing dictionary view controller, if it exists + if let refVC = self.referenceViewController + { + refVC.removeFromParentViewController() + refVC.view.removeFromSuperview() + } + + // Create the dictionary view controller + let refVC = UIReferenceLibraryViewController(term: searchText) + + // Display the dictionary view inside the container view + self.addChildViewController(refVC) + refVC.view.frame = self.dictionaryContainer.frame + self.view.addSubview(refVC.view) + refVC.didMoveToParentViewController(self) + + // Remember for later + self.referenceViewController = refVC + } + } + func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { // Delay slightly to make typing smoother Timer("type delay", 1.0) { - // Check if dictionary contains typed word - let searchText = searchBar.text - if UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm(searchText) - { - // Remove the existing dictionary view controller, if it exists - if let refVC = self.referenceViewController - { - refVC.removeFromParentViewController() - refVC.view.removeFromSuperview() - } - - // Create the dictionary view controller - let refVC = UIReferenceLibraryViewController(term: searchText) - - // Display the dictionary view inside the container view - self.addChildViewController(refVC) - refVC.view.frame = self.dictionaryContainer.frame - self.view.addSubview(refVC.view) - refVC.didMoveToParentViewController(self) - - // Remember for later - self.referenceViewController = refVC - } + self.searchDictionary() } } + func searchBarTextDidEndEditing(searchBar: UISearchBar) + { + searchDictionary() + } + func searchBarSearchButtonClicked(searchBar: UISearchBar) { // Hide keyboard