connectiondidFinishLoading调用了两次

时间:2013-02-25 06:16:55

标签: objective-c ipad xcode4.5

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

编辑代码:

再次连接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];
        currentHtmlElement=@"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];


    }

1 个答案:

答案 0 :(得分: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];