0

我正在尝试在下载和处理一些数据时将 MBProgressHUD 正确集成到项目中。如果我问愚蠢的事情,请原谅我的无知,但我是一个完全的菜鸟......

我有一个处理我的 HTTPRequests 的“数据”类,所以我认为这里是放置一些东西的正确位置:

-(void)resolve
{
    // Create the request.
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[[NSURL alloc] initWithString:[self url]]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
    NSLog(@"Resolving content from: %@",[self url]);
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [[NSMutableData data] retain];
    } else {
        NSLog(@"Content - resolve: connection failed");
    }

    // Here is the part that it makes me crazy...

    HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];

    return;

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    expectedLength = [response expectedContentLength];
    currentLength = 0;
    HUD.mode = MBProgressHUDModeDeterminate;
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    currentLength += [data length];
    HUD.progress = currentLength / (float)expectedLength;

    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.

    [receivedData appendData:data];
    return;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [HUD hide:YES];
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    // inform the user
    NSLog(@"Content - Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    return;
}

现在我知道将“showHUDaddTo”放在-(void)resolve中不是正确的方法......

我从一个视图控制器调用这个数据函数,它的 IBaction 如下所示:

-(IBAction) btnClicked:(id) sender {
    if ([[issue status] intValue] == 1 ) // issue is not downloaded
    {

        [(Content *)[issue content] resolve];
        //HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];




    }
    else // issue is downloaded - needs to be archived
    {
        NSError * error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:[(Content *)[issue content] path]  error:&error];
        if (error) {
            // implement error handling

        }
        else {
            Content * c = (Content *)[issue content];
            [c setPath:@""];
            [issue setStatus:[NSNumber numberWithInt:1]];
            [buttonView setTitle:@"Download" forState:UIControlStateNormal];
            [gotoIssue setTitle:@"..." forState:UIControlStateNormal];


            // notify all interested parties of the archived content
            [[NSNotificationCenter defaultCenter] postNotificationName:@"contentArchived" object:self]; // make sure its persisted!

        }



    }
}

简单地说:当我按下 IssueController.m 文件中的下载按钮时,我想从我的 Conten.m 文件中调用所有 MBProgressHUD 内容。我想您现在看到了我的问题所在:我不是编码员;-) 任何帮助表示赞赏。

干杯,

桑德尔

4

1 回答 1

0

我会尝试使用 HUD 方法 showWhileExecuting。用一种新方法包装您的调用,然后调用它来下载您的信息。当您的方法完成时,您的 HUD 也将完成。

此外,您是否有理由保留对 HUD 的呼吁?我以前没有见过,我不确定您是否需要保留它们,因为它们是自动发布的。

于 2012-01-30T14:48:45.417 回答