2013-02-25 58 views
0

我是iphone开发新手。我能够使用connectiondidFinishLoading方法中的数据。但我看到“连接didfinishloading”方法被称为两次。我不知道,我哪里出错了。这里是我的代码connectiondidFinishLoading调用两次

编辑的代码:

再次connectionDidFinishLoading叫了两声

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 

     if(textField==CompanyName) 
    { 

     autocompleteTableView.hidden = NO; 

     NSString *substring = [NSString stringWithString:textField.text]; 
     substring = [substring stringByReplacingCharactersInRange:range withString:string]; 
     [self searchAutocompleteEntriesWithSubstring:substring]; 
     return YES; 
     if([CompanyName.text length]==0) 
     { 
      autocompleteTableView.hidden = YES; 
      [popoverController dismissPopoverAnimated:YES]; 


     } 
    } 
    return YES; 
} 

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring 
{ 
    data = [[NSMutableData alloc] init]; 
    self.receivedData = data; 
    [data release]; 

    NSURL *jsonUrl =[NSURL URLWithString:[NSString stringWithFormat:@"http://xxxx=%@",substring]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:jsonUrl]; 

    connection = [[NSURLConnection alloc] initWithRequest:request                 delegate:self ]; 
    //self.connection = connection;a 
    } 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

{ 



    [receivedData setLength:0]; 

} 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

{ 

    [receivedData appendData:data]; 


} 



- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 

     parser = [[NSXMLParser alloc] initWithData:receivedData]; 
     [email protected]"5"; 
     [parser setDelegate:self]; 
     [parser setShouldProcessNamespaces:NO]; 
     [parser setShouldReportNamespacePrefixes:NO]; 
     [parser setShouldResolveExternalEntities:NO]; 
     [parser parse]; 
     [parser release]; 



     if([arr4 count]!=0) 
     { 
      self.autocompleteUrls = [[[NSMutableArray alloc] init]autorelease]; 


      UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController 


      self.autocompleteUrls = [[NSMutableArray alloc] init]; 

      viewForautoCompleteTableView = [[UIView alloc]initWithFrame:CGRectMake (410, 120,270, 250)]; 


      autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,270,250) style:UITableViewStyleGrouped]; 
      autocompleteTableView.delegate = self; 
      autocompleteTableView.dataSource = self; 
      autocompleteTableView.scrollEnabled = YES; 
      autocompleteTableView.backgroundColor = [UIColor lightTextColor]; 
      autocompleteTableView.rowHeight=28; 

      autocompleteTableView.backgroundView = nil; 
      autocompleteTableView.backgroundColor = [UIColor whiteColor]; 


      autocompleteTableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
      [autocompleteTableView setSeparatorColor:[UIColor orangeColor]]; 
      [viewForautoCompleteTableView setFrame:CGRectMake(210,380 ,autocompleteTableView.frame 
                   .size.width 
                   ,autocompleteTableView.frame.size.height)]; 

        [viewForautoCompleteTableView addSubview:autocompleteTableView]; 

      [viewForautoCompleteTableView setBackgroundColor:[UIColor whiteColor]]; 
      CGRect popoverFrame = viewForautoCompleteTableView.frame; 
      popoverContent.view = viewForautoCompleteTableView; 

      popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; 
      popoverController.delegate=self; 
      [popoverContent release];   
      [popoverController setPopoverContentSize:CGSizeMake(viewForautoCompleteTableView.frame.size.width, viewForautoCompleteTableView.frame.size.height) animated:NO]; 

         [popoverController presentPopoverFromRect:popoverFrame inView:testscroll permittedArrowDirections:0 animated:YES]; 




      [autocompleteUrls removeAllObjects]; 
      for(int i=0;i<[arr4 count];i++) 
      { 
       NSString *curString = [[arr4 objectAtIndex:i] valueForKey:@"Name"]; 
       [autocompleteUrls addObject:curString]; 
          } 




     } 
    [autocompleteTableView reloadData]; 
     [connection cancel]; 


    } 
+0

您是否多次调用'searchAutocompleteEntriesWithSubstring:'?如果这是每次用户输入搜索字段时,请确保在开始新连接之前取消当前的连接。如果用户输入更新的搜索字符串,最有可能不需要旧结果。 – rmaddy 2013-02-25 06:56:55

+0

我正在调用searchAutocompleteEntriesWithSubstring:只有一次。我们必须在开始搜索时取消当前连接,以及如何执行此操作? – Sindu 2013-02-25 07:06:06

回答

1

你应该删除[connection start];因为连接会后NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:selfstartImmediately:NO];

自动启动编辑 有效NSURLConnection初始化

NSURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@""]]; 

     [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
+0

删除“[连接开始];”didnot调用“connectiondidfinishloading” – Sindu 2013-02-25 07:00:37

+0

我不明白你的意见,澄清请删除后[连接开始] – 2013-02-25 07:06:46

+0

connectionDidfinishloading方法不呼吁 – Sindu 2013-02-25 07:09:37