0

iphone開発初心者です。メソッドでデータを使用できますが、connectiondidFinishLoading「connection didfinishloading」メソッドが 2 回呼び出されています。どこが間違っているのかわかりません。これが私のコードです

編集されたコード:

再び connectionDidFinishLoading が 2 回呼び出されました

 - (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 に答える