UISearchDisplayController 的正确实例化 [英] Proper instantiation of UISearchDisplayController

查看:44
本文介绍了UISearchDisplayController 的正确实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一些搜索,但我仍然不清楚答案.我正在尝试在 TableViewController (TVC) 中创建 UISearchDisplayController 的实例.

I did some searching and the answer is still unclear to me. I am trying to create an instance of a UISearchDisplayController inside a TableViewController (TVC).

在我的 TVC 的标题中,我将 searchDisplayController 声明为一个属性:

In the header of my TVC, I declared a searchDisplayController as a property:

@interface SDCSecondTableViewController : UITableViewController

@property (nonatomic, strong) NSArray *productList;
@property (nonatomic, strong) NSMutableArray *filteredProductList;
@property (nonatomic, strong) UISearchDisplayController *searchDisplayController;

@end

这样做会产生错误:

属性searchDisplayController"试图使用在超类UIViewController"中声明的实例变量_searchDisplayController"

Property 'searchDisplayController' attempting to use instance variable '_searchDisplayController' declared in super class 'UIViewController'

在实现文件中添加@synthesize searchDisplayController 消除了错误.

Adding @synthesize searchDisplayController in the implementation file got rid of the errors.

谁能帮我理解这个错误?我使用的是 Xcode 4.6.2,但我的印象是从 Xcode 4.4 开始自动合成属性.

Can anyone please help me understand this error? I'm using Xcode 4.6.2, but I was under the impression that properties are automatically synthesized starting with Xcode 4.4.

推荐答案

你不应该像 LucOlivierDB 建议的那样调用 [self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController];.这是一个私有 API 调用,它将让您的应用程序被 Apple 拒绝(我知道,因为它发生在我身上).而是这样做:

You shouldn't call [self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController]; as LucOlivierDB suggested. That is a private API call that will get your app rejected by Apple (I know because it happened to me). Instead just do this:

@interface YourViewController ()
    @property (nonatomic, strong) UISearchDisplayController *searchController;
@end

@implementation YourViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;

    self.tableView.tableHeaderView = self.searchBar;

}

这篇关于UISearchDisplayController 的正确实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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