UIViewController does not retain its programmatically-created UISearchDisplayController

后端 未结 2 1960
予麋鹿
予麋鹿 2020-12-23 14:33

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

If you create your search displa

相关标签:
2条回答
  • 2020-12-23 14:47

    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.

    0 讨论(0)
  • 2020-12-23 15:01

    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
    }
    
    0 讨论(0)
提交回复
热议问题