Iphone 如何使用URL从UISearchBar进行搜索

Iphone 如何使用URL从UISearchBar进行搜索,iphone,ios,objective-c,uisearchbar,Iphone,Ios,Objective C,Uisearchbar,我正在尝试直接从UISearchBar搜索。我在这方面遇到了一些问题,我想知道是否有任何教程可以涵盖这一点,或者是否有人可以提供帮助。这是我的密码: -(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if ([searchText length] == 0) { [displayItems removeAllObjects]; } else {

我正在尝试直接从UISearchBar搜索。我在这方面遇到了一些问题,我想知道是否有任何教程可以涵盖这一点,或者是否有人可以提供帮助。这是我的密码:

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    if ([searchText length] == 0) {
        [displayItems removeAllObjects];

    } else {
        [displayItems removeAllObjects];

        NSString *url=[NSString stringWithFormat:@"http://xml.customweather.com/xml?client=clinique_test&client_password=f@c3$toF&product=search&search=%@",searchBar.text];
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];

        }
    }

    // reload the table data
    [self.tableView reloadData];
} 

从您的代码中,您可以使用
URLConnection
UISearchBar
中对每个字符进行更改,因此它并不适合您

注意

在这里,您希望从web服务器获取数据,然后在
UISearchBar
的文本字段中输入整个字符串后,在这里发送请求。。与
searchBarSearchButtonClicked
方法类似

//RootViewController.m
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

   [self searchTableView];
   [searchTableView reloadData];
}

- (void) searchTableView {

    NSString *url = [NSString stringWithFormat:@"http://xml.customweather.com/xml?client=clinique_test&client_password=f@c3$toF&product=search&search=%@",searchBar.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
}
试试这个

 - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
    {
        NSString *trimDot = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([trimDot isEqualToString:@"."]) {
            return YES;
        }

    if(connection){
        [connection cancel];
    }
    NSString *appendStr;
       if([text length] == 0)
    {
        NSRange rangemak = NSMakeRange(0, [searchbar.text length]-1);
        appendStr = [searchbar.text substringWithRange:rangemak];
    }
    else
    {
        appendStr = [NSString stringWithFormat:@"%@%@",searchbar.text,text];

    }
    [self performSelector:@selector(callSearchWebService:) withObject:appendStr];

    [activityindicator startAnimating];
    return YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [self performSelector:@selector(callSearchWebService:) withObject:searchBar.text];
    searchBar.showsCancelButton=NO;

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{   
    [self performSelector:@selector(callSearchWebService:) withObject:searchBar.text];
    [searchBar resignFirstResponder];
}

-(void)callSearchWebService:(NSString*)searchStr
{

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

        NSString *str =@"";
        if ([searchStr length] != 0 ) str = searchStr;


        NSString *url = [NSString stringWithFormat:@"%@/%d/%d/%@/",ListoutForSearcing_server,minRecords,maxRecords,str];
        [request setURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        // Create Connection.
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if (connection) {
            JsonResponseData = [NSMutableData data] ;
            NSLog( @"Data will be received from URL: %@", request.URL );
        }
        else
        {// The download could not be made.
            NSLog( @"Data could not be received from: %@", request.URL );
        }
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    HUD.delegate = self;
    HUD.labelText = @"Loading";
    [HUD show:YES];
}
连接委托方法

#pragma mark Connection delegate methods.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [JsonResponseData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [JsonResponseData appendData:data];   

}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error
{
    UIAlertView *alertError=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Network Problem." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertError show];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    receivedString = [[NSString alloc] initWithData:JsonResponseData 
                                           encoding:NSASCIIStringEncoding];
    RecievedDataDict=[[NSMutableDictionary alloc]init];

}

我不太确定我是否理解您在做什么,如果用户搜索(我猜是一个城市),它将发送一个天气请求,您将使用返回到表视图中的数据。这里出了什么问题?您的NSURLConnection实现在哪里?您希望此代码做什么?我知道在这个时候它不应该做任何事情。我想知道是否有一些示例代码我可以遵循,有人知道。我真的需要一些参考资料来推动这项工作。谢谢:)谢谢:)不过这是在搜索nsarray。我在寻找一种可以搜索在线服务,然后在tableView中显示结果的东西。我把便条放在bro。。请参见此处了解您的要求您在用户在文本字段中搜索整个字符串后发送此请求,当用户单击搜索按钮然后发送此请求时,请阅读教程,从中了解一些信息……教程的链接已失效。