无法在iPhone应用程序上显示活动指示器
我有一个在IB中创建的视图。在它里面我有一个以编程方式创建的滚动视图。在滚动视图中,我连接到web服务并获取内容和图像。我想在执行此操作时显示活动指示器。因此,我:无法在iPhone应用程序上显示活动指示器,iphone,objective-c,uiactivityindicatorview,Iphone,Objective C,Uiactivityindicatorview,我有一个在IB中创建的视图。在它里面我有一个以编程方式创建的滚动视图。在滚动视图中,我连接到web服务并获取内容和图像。我想在执行此操作时显示活动指示器。因此,我: - (void)viewDidLoad { [super viewDidLoad]; activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]
- (void)viewDidLoad
{
[super viewDidLoad];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center = self.view.center;
[self.view.window addSubview:activityIndicator];
在添加scrollview之后,我有:
[self.view addSubview:scrollView];
// ADD Scroll View Ends
//Add the Lisitng Image
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(loadImage)
object:nil];
[queue addOperation:operation];
在loadimage中,我有:
- (void)loadImage {
[activityIndicator startAnimating];
我尝试过[self.view.window addSubview:activityIndicator];,[自->滚动视图添加子视图:活动指示器];,[self.view addSubview:activityIndicator]
但我就是不能让指示器显示出来。有人知道什么地方出了问题吗?你应该在你的视图中这样做
- (void)viewDidLoad
{
//Start Activity Indicator View
indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicatorView.frame = CGRectMake(40.0, 20.0, 60.0, 60.0);
indicatorView.center = self.view.center;
[self.view addSubview:indicatorView];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[indicatorView startAnimating];
[self performSelectorInBackground:@selector(loadscroll) withObject:self];
}
注意:“performSelectorInBackground”将在视图上显示活动指示器,loadScroll方法将从internet获取所有数据并执行其他工作
-(void)loadscroll
{
//Your Scroll View
//Your other Data Manipulation even from internet
//When data is fetched display it
[self removeActivityIndicator]; //To stop activity Indicator
}
- (void)removeActivityIndicator
{
[indicatorView stopAnimating];
}
你应该在你的视图中这样做
- (void)viewDidLoad
{
//Start Activity Indicator View
indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicatorView.frame = CGRectMake(40.0, 20.0, 60.0, 60.0);
indicatorView.center = self.view.center;
[self.view addSubview:indicatorView];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[indicatorView startAnimating];
[self performSelectorInBackground:@selector(loadscroll) withObject:self];
}
注意:“performSelectorInBackground”将在视图上显示活动指示器,loadScroll方法将从internet获取所有数据并执行其他工作
-(void)loadscroll
{
//Your Scroll View
//Your other Data Manipulation even from internet
//When data is fetched display it
[self removeActivityIndicator]; //To stop activity Indicator
}
- (void)removeActivityIndicator
{
[indicatorView stopAnimating];
}
在多次面对活动指示器的上述问题之后,我发现了什么
- 活动指示器和活动指示器出现后需要执行的函数(或方法或代码段)都不能在主线程上运行。如果是这样,活动指示器将不会出现。解决方案是在主线程上运行ActivityIndicator,在后台线程中运行函数
- 我还遇到过这样一种情况:活动指示器后面的函数不能在后台线程中运行。在我的例子中,它是使用MPMoviePlayerController播放一个不能在后台线程中执行的音乐文件。我解决这个问题的方法是在主线程中运行activity indicator,然后使用NSTimer在延迟后调用我的方法,该延迟足以让activity indicator出现并开始制作动画
- 我还发现,必须在performSelectorOnMainThread中调用activity indicator,并在给定viewController开始设置动画后,将其作为子视图添加到其顶视图中。 一旦不需要它,就应该通过调用removeFromSuperView方法将其删除
- 活动指示器和活动指示器出现后需要执行的函数(或方法或代码段)都不能在主线程上运行。如果是这样,活动指示器将不会出现。解决方案是在主线程上运行ActivityIndicator,在后台线程中运行函数
- 我还遇到过这样一种情况:活动指示器后面的函数不能在后台线程中运行。在我的例子中,它是使用MPMoviePlayerController播放一个不能在后台线程中执行的音乐文件。我解决这个问题的方法是在主线程中运行activity indicator,然后使用NSTimer在延迟后调用我的方法,该延迟足以让activity indicator出现并开始制作动画
- 我还发现,必须在performSelectorOnMainThread中调用activity indicator,并在给定viewController开始设置动画后,将其作为子视图添加到其顶视图中。 一旦不需要它,就应该通过调用removeFromSuperView方法将其删除