如何获取Searchbar切换显示/隐藏基于IBAction值的范围栏?

时间:2014-05-21 22:00:19

标签: ios objective-c uisearchbar

如何将Searchbar值和Scope Bar Value添加到变量中 所以我可以将它作为GET值发送到REST API,如

SEARCHTERM rateType

我有以下代码。

- (IBAction)showSearchOptions:(id)sender {    
    if(toggle == 0){
        [self.searchBar sizeToFit];
        self.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"High Rate", @"Low Rate", nil];
        self.searchBar.showsScopeBar = YES;
        [self.searchBar sizeToFit];
        toggle = 1;

    } else {

        self.searchBar.scopeButtonTitles = nil;
        [self.searchBar sizeToFit];
        toggle = 0;
    }

  }

2 个答案:

答案 0 :(得分:0)

对此的答案如下:如果整数声明正确,请你纠正吗?基于编码标准。这有效,但我希望永远变得更好。谢谢。我看到了文档

文档范围栏:https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBar_Class/Reference.html

SearchBar委托:https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarDelegate_Protocol/Reference/Reference.html

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *searchTerm = searchBar.text;
int rateLevel = searchBar.selectedScopeButtonIndex;

NSString *rateString = [NSString stringWithFormat:@" Your Search Term is '%@' and Your Rate Type is '%d'", searchTerm, rateLevel];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hey!" 
message:rateString 
delegate:self 
cancelButtonTitle:@"Cancel" 
otherButtonTitles:@"Try Again",nil];

[alert show];


}

答案 1 :(得分:0)

也许您需要self.searchbar.text这样的内容,而且通常是NSPredicate

过滤
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{    
    NSPredicate *predi = [NSPredicate predicateWithFormat:@"tags CONTAINS[c] %@", self.searchBar.text];
}