iPhone Objective-C以编程方式将范围按钮添加到UISearchBar

时间:2010-07-28 10:17:23

标签: iphone ios objective-c scope uisearchbar

我目前用于创建UISearchBar的这段代码(改编自之前的stackoverflow示例):

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    [searchBar sizeToFit];

    //searchBar.delegate = self;
    searchBar.placeholder = @"Search messages, listeners or stations";
    self.tableView.tableHeaderView = searchBar;

    UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    // The above assigns self.searchDisplayController, but without retaining.
    // Force the read-only property to be set and retained. 
    [self performSelector:@selector(setSearchDisplayController:) withObject:searchDC];

    //searchDC.delegate = self;
    //searchDC.searchResultsDataSource = self;
    //searchDC.searchResultsDelegate = self;

    [searchBar release];
    [searchDC release];

我需要在工具栏的底部添加3个范围按钮:“主题”,“消息”,“工作站”并默认选择第一个。有人可以告诉我怎么做吗?

1 个答案:

答案 0 :(得分:16)

哦..没关系..发现它..

searchBar.showsScopeBar = YES;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags", @"Listeners", @"Stations", nil];