UIViewController不保留其以编程方式创建的UISearchDisplayController [英] UIViewController does not retain its programmatically-created UISearchDisplayController

查看:83
本文介绍了UIViewController不保留其以编程方式创建的UISearchDisplayController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIViewController 文档关于 searchDisplayController 属性 1 它说:

如果以编程方式创建搜索显示控制器,搜索显示控制器在初始化时会自动设置此属性。

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] 不是。但是,它在事件循环结束后被填零,这会导致搜索显示控制器在我在搜索栏内部触摸时不显示。什么都没有崩溃。这很奇怪。如果我省略对 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 (我验证了)这与仪器)。由于 searchDisplayController property 被标记为(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 C $ C>。否则这很好。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆