将 MBProgressHUD 与两个类集成

发布于 2024-12-10 05:04:21 字数 3864 浏览 0 评论 0原文

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

我有一个“Data”类正在处理我的 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!

        }



    }
}

简单地说:我想调用所有 MBProgressHUD 内容从当我按下 IssueController.m 文件中的下载按钮时,我的 Conten.m 文件。我想你现在明白我的问题出在哪里:我不是编码员;-)任何帮助都是值得赞赏的。

干杯,

桑德尔

Im am trying to properly integrate MBProgressHUD in a project while i am downloading and processing some data. Forgive me my ignorance if i am asking stupid things, but i'm a complete noob...

I have a "Data" class that is handling my HTTPRequests, so i thing here is the proper place to put some stuff in:

-(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;
}

Now i know that putting the "showHUDaddedTo" in the -(void)resolve is not the right way...

I am calling this data function from a view controller with an IBaction like this:

-(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!

        }



    }
}

Simply said: i would like to call all the MBProgressHUD stuff from my Conten.m file in the moment i push the download button in my IssueController.m file. I think you see now where my problem is: i am not a coder ;-) Any help is appreciated.

Cheers,

sandor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ弥猫深巷。 2024-12-17 05:04:21

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

另外,您保留对 HUD 的电话有什么原因吗?我以前没有见过这种情况,我不确定您是否需要保留它们,因为它们是自动释放的。

I would try and use the HUD method showWhileExecuting. Wrap your calls up in a new method and call that to download your information. When your method finishes, so will your HUD.

Also, is there a reason why you are retaining your calls for the HUD? I have not seen that before and I'm not sure if you need to retain those as they are autoreleased.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文