                                         animated: true)
     }
     
+    override func viewWillDisappear(_ animated: Bool) {
+        super.viewWillDisappear(animated)
+        
+        self.searchController.isActive = false
+    }
+    
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
-        self.searchController.isActive = false
-        self.dismissSearchBar()
-        
         guard let validSegueIdentifier = segue.identifier, let validSegueIdentifierCase = NotesTableViewControllerSegue(rawValue: validSegueIdentifier) else {
             assertionFailure("Could not map segue identifier: \(String(describing: segue.identifier))")
             return

            guard let validNotesViewController = segue.destination as? NotesViewController,
                let selectedNoteCell = sender as? NotesTableViewCell,
                let selectedIndexPath = self.tableView.indexPath(for: selectedNoteCell) else { return }
            let selectedNote = self.noteDocuments[selectedIndexPath.row].note
            validNotesViewController.note = selectedNote
        case .segueToNotesViewControllerFromAddButton:
            print("adding new note")

        self.dismissSearchBar()
    }
}

// MARK: - UISearchResultUpdating Protocol

extension NotesTableViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        guard let searchBarText = searchController.searchBar.text else { return }
        self.filterContentForSearchText(searchBarText)
    }
}
