0

我是 iphone 开发的新手。我可以在方法中使用数据。但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];
        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];


    }
4

1 回答 1

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];
于 2013-02-25T06:51:26.970 回答