UIViewController 不保留其以编程方式创建的 UISearchDisplayController [英] UIViewController does not retain its programmatically-created UISearchDisplayController
问题描述
在 UIViewController
文档关于 searchDisplayController
属性 1 它说:
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.
当我这样创建 UISearchDisplayController 时:
And when I create my UISearchDisplayController thusly:
[[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
-[UIViewController searchDisplayController]
不是 nil
.但是,它在事件循环完成后被清除,这会导致当我触摸搜索栏内部时搜索显示控制器不显示.什么都不会崩溃.这很奇怪.如果我省略对 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];
然而,泄漏了 UISearchDisplayController
(我用 Instruments 验证了这一点).由于 searchDisplayController
属性被标记为(nonatomic, retain, readonly)
我希望它在设置后会保留 UISearchDisplayController
.
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.
这篇 stackoverflow 文章是相关的.
推荐答案
我遇到了同样的事情.我以编程方式创建了我所有的控制器/视图.一切正常,直到我将我的项目转换为使用 ARC.一旦我这样做了 UISearchDisplayControllers
不再保留,并且每个 UIViewController
中的 searchDisplayController
属性在运行循环结束后为零.
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.
我不知道为什么会发生这种情况.Apple 文档建议视图控制器应保留 SDC,但这显然不会发生.
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.
我的解决方案是创建第二个属性来保留 SDC,并在卸载视图时将其置零.如果您不使用 ARC,则需要在 viewDidUnload
和 dealloc
中释放 mySearchDisplayController
.否则就这样就好了.
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.
在 MyViewController.h 中:
In MyViewController.h:
@property (nonatomic, strong) UISearchDisplayController * mySearchDisplayController;
在 MyViewController.m 中:
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
}
这篇关于UIViewController 不保留其以编程方式创建的 UISearchDisplayController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!