UIViewController does not retain its programmatically-created UISearchDisplayController

試著忘記壹切 提交于 2019-11-28 16:46:37

问题


In the UIViewController documentation about the searchDisplayController property 1 it says:

If you create your search display controller programmatically, this property is set automatically by the search display controller when it is initialized.

And when I create my UISearchDisplayController thusly:

[[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];

-[UIViewController searchDisplayController] is not nil. However, it is nilled out after the event loop finishes, which causes the search display controller not to show when I touch inside the search bar. Nothing crashes. This is very weird. If I omit the call to autorelease, everything works:

[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

However, leaks the UISearchDisplayController (I verified this with Instruments). Since the searchDisplayController property is marked as (nonatomic, retain, readonly) I expect that it would retain the UISearchDisplayController after it is set.

This stackoverflow article is related.


回答1:


I've run into the same thing. I create all of my controllers/views programmatically. Everything was working fine until I converted my project to use ARC. Once I did the UISearchDisplayControllers were no longer retained and the searchDisplayController property in each UIViewController was nil after the run loop ended.

I don't have an answer why this is happening. The Apple docs suggest that the SDC should be retained by the view controller but this is clearly not happening.

My solution was to create a second property to retain the SDC and I nil it when I unload the view. If you are not using ARC you need to release mySearchDisplayController in viewDidUnload and dealloc. Otherwise this is good as is.

In MyViewController.h:

@property (nonatomic, strong) UISearchDisplayController * mySearchDisplayController;

In MyViewController.m:

@synthesize mySearchDisplayController = _mySearchDisplayController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // create searchBar
    _mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    _mySearchDisplayController.delegate = self;
    _mySearchDisplayController.searchResultsDataSource = self;
    _mySearchDisplayController.searchResultsDelegate = self;
    // other stuff
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    _mySearchDisplayController = nil;
    // other stuff
}



回答2:


The solution above works just fine, but I also found that you can use

[self setValue:mySearchDisplayController forKey:@"searchDisplayController"]

in the context of a UIViewController subclass.



来源:https://stackoverflow.com/questions/7679501/uiviewcontroller-does-not-retain-its-programmatically-created-uisearchdisplaycon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!