2013-02-25 93 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