我正在尝试实现SVProgressHUD进度活动指示器。我从[演示]中复制了该类。1
我的应用程序已加载,但活动指示器未显示。这是我第一次尝试使用其中一个,所以任何帮助都将不胜感激。
这是代码:
#import "SVProgressHUD.h"
@implementation QuotesAppDelegate
- (void)startLoading
{
//call this in your app delegate instead of setting window.rootViewController to your main view controller
//you can show a UIActivityIndiocatorView here or something if you like
[SVProgressHUD show];
[self performSelectorInBackground:@selector(loadInBackground) withObject:nil];
}
- (void)loadInBackground
{
//do your loading here
//this is in the background, so don't try to access any UI elements
[self populateFromDatabase];
[SVProgressHUD dismiss];
[self performSelectorOnMainThread:@selector(finishedLoading) withObject:nil waitUntilDone:NO];
}
- (void)finishedLoading
{
//back on the main thread now, it's safe to show your view controller
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self copyDatabaseIfNeeded];
[self startLoading];
}
对于其他有类似问题的人来说,这也可能发生,因为你有一个长循环或一段代码需要很长时间才能执行。如果发生这种情况,你的进度条直到循环结束后才会显示,这有点违背目的。
要解决这个问题,你需要给你这个:
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
基本上,你的代码看起来像这样:
- (IBAction)submitPost:(id)sender {
//now we show the loading bar and submit the comment
[SVProgressHUD showWithStatus:@"Submitting post" maskType:SVProgressHUDMaskTypeGradient];
SEL aSelector = @selector(submitDataOfPost);
[self performSelectorInBackground:aSelector withObject:sender];
}
这将基本上加载进度条,并且在后台线程中,将调用要执行的方法。这样可以确保在执行代码的同时更新UI(显示进度hud)。
首先,您没有将SVProgressHUD添加到视图中。
如果您的类继承自UIViewController,则[self.view addSubview:];或者如果你的类是简单的UIView,那么[self-addSubView:]
我不理解你的要求,但据我所知,通过你的代码,你正在显示[SVProgressHUD显示];在startLoading方法中,然后在该方法中调用loadInBackground方法,使用[SVProgressHUD disse]隐藏hud;
我建议您使用断点来跟踪它,并找出它。
我也遇到了同样的问题。当我将SVProgressHUD的一个版本更改为后一个版本时,问题消失了。我当前的版本支持ARC.
=>
(IBAction)fetchData:(id)sender
{
[SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeGradient];
[self performSelectorOnMainThread:@selector(getDataFromSomeWhere) withObject:nil waitUntilDone:NO];
}
=>
(void)getDataFromSomeWhere
{
//Do your data populating here. and dismiss the ProgressHud.
[SVProgressHUD dismiss];
}