Question

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 http://img192.imageshack.us/img192/3050/screenshot20100307at304.png

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???

Was it helpful?

Solution

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)?

OTHER TIPS

Usually child objects shouldn't retain its parent. In this case its the parent controller that should retain the child (which is the search display controller).

This has been done for you automatically when you're creating the SDC in NIB file because it has been added to the view controller's searchDisplayController property and thus retained for the view controller's lifetime.

However since setting the searchDisplayController property on a view controller is considered usage of private api. You should just add an ivar to retain it and release it on dealloc manually.

Simply removing the autorelease call is a memory leak as you're leaving around an object you init-ed without saving a reference to it so I don't think it is the correct answer.

Instead you should retain the searchController properly in an ivar and release it on properly on dealloc as you would any object you want to stay alive for the lifetime of the view controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top