views:

343

answers:

2

I'm working on a tab bar application and one of the tabs has a UISearchDisplayController hooked up to a UISearchBar. It's all connected up in the NIB and is working. When I tap the search bar, the Scope and Cancel buttons fly in etc, and the search delegate updates the results table correctly.

However, I'm trying to implement the same code in the viewDidLoad message instead of the NIB, however when I delete the search display controller from the NIB and uncomment my code to create the same controller in the function, it doesn't work. It's as if there's some fundamental connection not being made so that all my search delegate functionality isn't being called.

Here's my working NIB version of the Search Display Controller. It's hooked up to the search bar, the UINavigationController subclass (MASearchController) and the root view of that is hooked up as the searchContentsController.

alt text

Now this is what you would expect to do in code to create the same, right? What I'm doing is leaving the UISearchBar in the NIB to eliminate one piece of the puzzle at a time in code.

// [MASearchController viewDidLoad]
UISearchDisplayController *searchController = [[[UISearchDisplayController alloc]
    initWithSearchBar:searchBar
    contentsController:[[self viewControllers] objectAtIndex:0]] autorelease];
[searchController setDelegate:self];
[searchController setSearchResultsDelegate:self];
[searchController setSearchResultsDataSource:self];

I've checked all objects at run time and they all check out. Essentially I've deleted the search display controller from the NIB and then put in the code to create it in the viewDidLoad message.

Why would this not work? The search keyboard comes up but none of my search and button animation functionality work???

+2  A: 

Wow, I just figured out the problem.

I figured, because the searchDisplayController property for the UIViewController is set inside the initWithSearchBar:contentsController: message I would still autorelease my copy of the pointer, but when I removed the autorelease the stupid thing started working. Gaaaah. Why would it not retain its own copy (the UIViewController)?

Nick Bedford
A: 

Thank you! You've just solved my hours and hours of debugging and searching. It's the same situation if one programatically creates a UIView with a child UITableView.

hyperphonic