简体   繁体   English

UISearchDisplayController保留周期

[英]UISearchDisplayController retain cycle

I have a very strange problem and I cannot find a solution. 我有一个非常奇怪的问题,找不到解决方案。 In one of my apps I need to create a UISearchDisplayController programatically. 在我的一个应用程序中,我需要以编程方式创建UISearchDisplayController I am creating it in a subclass of UITableViewController . 我在UITableViewController的子类中创建它。 And I run into a very simple problem - my search display controller either gets released immediately OR it causes the retain cycle and prevents its contents controller from getting released. 我遇到了一个非常简单的问题-我的搜索显示控制器要么立即被释放,要么导致保留周期并阻止其内容控制器被释放。

In my viewDidLoad method I instantiate my UISearchDisplayController with this code: 在我的viewDidLoad方法中,我使用以下代码实例化UISearchDisplayController

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
sC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;

If sC is defined as a property or instance variable in my view controller, search controller works, but it prevents dealloc method of my view controller from being called. 如果在我的视图控制器中将sC定义为属性或实例变量,则搜索控制器可以工作,但可以防止调用我的视图控制器的dealloc方法。 If, however, sC is defined as the variable only within viewDidLoad method, my view controller gets deallocated fine, but self.searchDisplayController becomes nil almost instantly and search doesn't work. 但是,如果sC被定义为仅在变量viewDidLoad方法,我的视图控制器被释放罚款,但self.searchDisplayController变为nil几乎立即和搜索无法正常工作。

Does anyone know how to solve this? 有谁知道如何解决这个问题? I have already tried overriding the searchDisplayController property - it doesn't help. 我已经尝试覆盖searchDisplayController属性-它没有帮助。

I should probably mention that I am using ARC. 我可能应该提到我正在使用ARC。 Also, when I say that " dealloc isn't called" I mean that I have an NSLog statement there which doesn't get printed. 另外,当我说“未调用dealloc ”时,我的意思是我那里有一个NSLog语句,该语句不会被打印。

Update 更新资料

As some users suggested that there is already an answer and that simply overriding the searchDisplayController property should work I post below what I did (which didn't work). 正如一些用户建议的那样,已经存在一个答案,并且简单地覆盖searchDisplayController属性应该可以工作,我在所做的事情下方发布了该内容(无效)。

I added a property to my subclass of UITableViewController : 我在UITableViewController子类中添加了一个属性:

@property (nonatomic,strong) UISearchDisplayController *searchDisplayController;

In my viewDidLoad I initialised my search controller: 在我的viewDidLoad我初始化了搜索控制器:

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;

In the dealloc method of my view controller I have: 在我的视图控制器的dealloc方法中,我有:

- (void)dealloc
{
    NSLog(@"dealloc");
    self.searchDisplayController = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Line "dealloc" doesn't get printed and if I profile with instruments my view controller is not getting released. 行“ dealloc”不会打印,并且如果我使用仪器进行配置文件,则不会释放视图控制器。

I have also tried adding other properties with different name - it still doesn't get deallocated. 我还尝试添加其他名称不同的属性-仍然不会取消分配。

haha have you ever go into that question I provided. 哈哈,你有没有想过我提供的那个问题。 So summary of that question's accepted answer: 因此,该问题的答案汇总:

  1. use a property to hold your UISearchDisplayController 使用属性保存您的UISearchDisplayController
  2. when the holder viewController is dealloc nil it out 当持有者viewController取消分配时,将其淘汰

     - (void)dealloc { self.searchDisplayController = nil; } 
  3. don't use UIViewController 's default property searchDisplayController it's broken 不要使用UIViewController的默认属性searchDisplayController它已损坏

Ok, I have figured it out. 好的,我知道了。 In order for dealloc to be called on the view controller which initialises the UISearchDisplayController I had to do this: 为了在初始化UISearchDisplayController的视图控制器上调用dealloc,我必须这样做:

At the top of .m file add the following: 在.m文件顶部,添加以下内容:

static UISearchDisplayController *sC;

Then in viewDidLoad initialise the search controller like this: 然后在viewDidLoad中初始化搜索控制器,如下所示:

    if (sC==nil) {
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        sC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    }
    self.searchDisplayController.searchResultsDelegate = self;
    self.searchDisplayController.searchResultsDataSource = self;
    self.searchDisplayController.delegate = self;

And then in the dealloc method do the following: 然后在dealloc方法中执行以下操作:

- (void)dealloc
{
    sC = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

And now it all works great. 现在一切正常。 Search controller is working fine and everything gets deallocated as it should :) 搜索控制器工作正常,一切都按需释放:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM