0
votes

I'm having difficulty following directions. I have a UISearchBar in a UIView. The user will enter the search string into the UISearchBar and click the search icon to search. The results will display in a new window (UITableView).

My search has shown me this:

A UISearchDisplayController cannot be added to a UIView because it doesn't inherit from a UIView. You can add a UISearchBar in Interface builder with an IBOutlet and then create a UISearchDisplayController with that UISearchBar programmatically.

Just do it in code e.g. (assuming the view controller is vc): [vc addSubview:mySearchDisplayController.searchBar]; // Note that searchBar is the view, and mySearchDisplayController only CONTROLS the searchBar etc.

and also this:

Just make your view controller implement the UISearchBarDelegate. In your xib file, all you need to do is to add a UISearchBar to your view and configure it as necessary, create an outlet for it (optional really but helps to be explicit), and assign the delegate outlet to your view controller. Then, to respond to the search bar events, implement the UISearchBarDelegate protocol methods as necessary. For example: - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [self handleSearch:searchBar]; }

  • (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { [self handleSearch:searchBar]; }

  • (void)handleSearch:(UISearchBar *)searchBar { NSLog(@"User searched for %@", searchBar.text); [searchBar resignFirstResponder]; // if you want the keyboard to go away }

  • (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar { NSLog(@"User canceled search"); [searchBar resignFirstResponder]; // if you want the keyboard to go away }

I'm just not getting it! Should I be adding mySearchController to my UIView? or my UISearchBar? Adding it to my UIView, nothing happens; adding it to my UISearchBar really wigs out the application. I don't even get an error - it just hangs.

Then, there is the second part: The delegate. Should I put the delegate in my UIView? Or in the UISearchDisplayController? Not sure which direction to go in and nothing so far is working. Please help.

All I really want at this point is just to get the handleSearch method to get executed. Thank you very much in advance for any help.

Very confused.

1
Do you need a uisearchdisplaycontroller? Or do you just want a UISearchBar?Hackmodford
Not sure if I need a uisearchdisplaycontroller. I have a uiviewcontroller and a uisearchbar in my xib. I'm just trying to get a method to fire when user presses the search icon. Thank you.Patricia
K. The UISearchDisplayController has it's own tableview that will show results. So when you press the searchbar with your finger it will show a cancel button, take up more of the screen, and show a blank tableview that says "no results". If you simply want a UISearchBar then just use a uisearchbar and watch for it's delegate method for when the search button is pressed.Hackmodford

1 Answers

0
votes

The idea is that you create a Search Bar in Interface Builder as an outlet and then use the following code to create UISearchDisplayController:

self.searchDisplayController2 = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController2.delegate = self;
self.searchDisplayController2.searchResultsDelegate = self;
self.searchDisplayController2.searchResultsDataSource = self;

This code would be implemented in your View Controller, say in viewDidLoad. You will also have to implement at least two delegate functions in the same VC:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;