我一开始就为这个冗长的问题道歉。
在我的应用程序中,我下载了大视频文件 (30-60Mb)。我显然想告诉用户进度。下载来自 Urban Airship,我使用他们的一种方法来获取进度。然而,这发生在 TableViewController 中,但下载指示器 (MBProgressHUD) 从另一个视图开始,称为 UADetail。
为了将进度从一个视图传递到另一个视图,我使用了 Singleton。随机地,它可能发生得早、晚或根本不发生,应用程序在日志中崩溃;
2012-03-12 15:13:30.528 isengua-en[3478:681f] HUD 课程下载进度:0.053478 2012-03-12 15:13:30.553 isengua-en[3478:707] 课程进度:0.055272 2012-03-12 15 :13:30.562 isengua-en[3478:707] LLVC downHUD 进度:0.055272 2012-03-12 15:13:30.565 isengua-en[3478:707] -[LessonListViewController >productsDownloadProgress:count:] [第 57 行] [StoreFrontDelegate ] productsDownloadProgress: 0.055272 count: 1 2012-03-12 15:13:30.569 isengua-en[3478:6307] * -[CFNumber _getValue:forType:]: 消息发送到解除分配的实例 0x83c6e80
首先是LessonListViewController;
- (void)productsDownloadProgress:(float)progress count:(int)count
{
DataManager *sharedManager = [DataManager sharedManager];
sharedManager.downHUD = [NSNumber numberWithFloat:progress];
NSLog(@"Lessonlist progress: %f", progress);
NSLog(@"LLVC downHUD progress: %f", [sharedManager.downHUD floatValue]);
UALOG(@"[StoreFrontDelegate] productsDownloadProgress: %f count: %d", progress, count);
if (count == 0) {
NSLog(@"Downloads complete in LessonListView!");
}
}
单例看起来像这样;
@implementation DataManager
@synthesize downHUD;
+ (DataManager *)sharedManager
{
static DataManager *sharedManager = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedManager = [[self alloc] init];
});
return sharedManager;
}
- (id)init {
if (self = [super init]) {
downHUD = [NSNumber numberWithFloat:(float)0];
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
NSLog(@"dealloc called in DataManager");
}
@end
然后在 UADetail 中读取它;
- (void)showWithLabelDeterminate {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
// Set determinate mode
HUD.mode = MBProgressHUDModeIndeterminate;
HUD.delegate = self;
HUD.labelText = NSLocalizedString(@"Waiting","");
// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(lessonDownloadProgress) onTarget:self withObject:nil animated:YES];
}
-(void)lessonDownloadProgress
{
DataManager *sharedManager = [DataManager sharedManager];
HUD.mode = MBProgressHUDModeDeterminate;
HUD.progress = [sharedManager.downHUD floatValue];
HUD.labelText = NSLocalizedString(@"DownLoading","");
while (HUD.progress < 1)
{
[self parentViewController];
NSLog(@"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.progress = [sharedManager.downHUD floatValue];
NSString *percent = [NSString stringWithFormat:@"%.0f", HUD.progress/1*100];
HUD.detailsLabelText = [percent stringByAppendingString:@"%"];
}
}