我正在尝试在下载和处理某些数据时正确地将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;
}
现在我知道将“showHUDaddedTo”放在 - (void)解析中是不正确的方法......
我正在使用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内容。我想你现在看到我的问题所在:我不是编码员;-)任何帮助都表示赞赏。
干杯,
桑多尔
答案 0 :(得分:0)
我会尝试使用HUD方法showWhileExecuting。用新方法包裹你的电话并打电话给你下载你的信息。当你的方法结束时,你的HUD也将完成。
另外,您是否有理由保留对HUD的呼叫?我以前没见过,我不确定你是否需要保留这些,因为它们是自动释放的。