In here说使用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];
}进度指示器在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];
}但还是不走运。有没有人告诉我我的不足之处?
附言:savePage save正在将所有网站内容保存到本地目录。我希望一旦保存完成,progressHUD就会消失。
谢谢
发布于 2012-04-02 02:49:43
尝试在viewWillAppear:上而不是-(IBAction)save上分配HUD,因为有时分配会占用整个时间,并且在分配时整个任务都会完成。
将以下链接复制到viewWillAppear:并从-(IBAction)save中删除它们
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Saving...";编辑:保留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];
}这将做的是创建一个单独的线程来显示HUD,因为主线程正在被savingPage方法占用。
如果这也不起作用,那么只需将waitUntilDone:YES更改为waitUntilDone:NO
备注:根据Apple documentation进行
重要信息:如果使用自动引用计数(ARC),则不能直接使用自动释放池。相反,您可以使用@autoreleasepool块。例如,代替:
NSAutoreleasePool *pool = [NSAutoreleasePool分配初始化;//受益于本地自动释放池的代码。池释放;
你可以这样写:
@ autorelease a{ //受益于本地自动发布池的代码。}
@autoreleasepool池块比直接使用NSAutoreleasePool的实例效率更高;即使不使用ARC,您也可以使用它们。
希望这能有所帮助。
发布于 2012-04-02 02:18:16
查看此代码
- (IBAction)save{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.graceTime = .1;
HUD.navigationBar = self.navigationController.navigationBar;
HUD.labelText = @"Saving";
HUD.delegate = self;
[self.view addSubview:HUD];
[HUD showWhileExecuting:@selector(savingPage) 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];
}当saveingpage方法完成时,将调用MBProgressHUD委托,因此在您的类中实现此委托方法,这将从您的视图中移除HUD
-(void)hudWasHidden
{
[HUD removeFromSuperview];
}发布于 2012-04-02 01:38:55
尝试将HUD分配行更改为
HUD = [MBProgressHUD showHUDAddedTo: self.navigationcontroller.view animated:YES];
HUD.labelText....然后在你的保存方法中,你可以
[MBProgressHUD hideHUDForView:self.navigationcontroller.view animated:NO];https://stackoverflow.com/questions/9966532
复制相似问题