dej*_*ong 0 iphone objective-c mbprogresshud
在这里说使用MBProgressHUD很容易.但我做不到.
这是我的代码:
- (IBAction)save{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
[HUD show:YES];
NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
[HUD hide:YES];
}
Run Code Online (Sandbox Code Playgroud)
savePage save方法运行期间未显示进度指示器,但在页面完全保存后显示(指示器显示不到1秒).
我也尝试过这种方式:
- (IBAction)save {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Saving...";
[HUD showWhileExecuting:@selector(performFetchOnMainThread) onTarget:self withObject:nil animated:YES];
}
- (void) savingPage{
NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
}
-(void) performFetchOnMainThread {
[self performSelectorOnMainThread:@selector(savingPage) withObject:nil waitUntilDone:YES];
}
Run Code Online (Sandbox Code Playgroud)
但仍然没有运气.有人让我知道我在这里缺少的地方吗?
PS:savePage save将所有网站内容保存到本地目录.我希望一旦保存完成,progressHUD就会消失.
谢谢
尝试的分配HUD上viewWillAppear:,而不是-(IBAction)save因为有时分配占据了整个时间和时它分配整个任务完成.
复制以下链接viewWillAppear:和从中删除-(IBAction)save
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Saving...";
Run Code Online (Sandbox Code Playgroud)
编辑:保持分配viewWillAppear:和更改代码,如下所示:
- (IBAction)save {
[NSThread detachNewThreadSelector:@selector(showHUD) withObject:nil];
[self performSelectorOnMainThread:@selector(savingPage) withObject:nil waitUntilDone:YES];
}
- (void) savingPage{
NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
[HUD hide:YES];
}
-(void)showHUD {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[HUD show:YES];
[pool release];
}
Run Code Online (Sandbox Code Playgroud)
这将做的是创建一个单独的线程来显示HUD,因为savePage方法正在使用主线程.
如果这也不起作用,那么只需waitUntilDone:YES改为waitUntilDone:NO
注意:根据Apple文档
要点:如果使用自动引用计数(ARC),则无法直接使用自动释放池.相反,您使用@autoreleasepool块代替.例如,代替:
Run Code Online (Sandbox Code Playgroud)NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init; // Code benefitting from a local autorelease pool. [pool release];你会写:
Run Code Online (Sandbox Code Playgroud)@autoreleasepool { // Code benefitting from a local autorelease pool. }@autoreleasepool块比直接使用NSAutoreleasePool实例更有效; 即使您不使用ARC,也可以使用它们.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
4884 次 |
| 最近记录: |