不能获取活动指示器以显示在iPhone应用程序上 [英] Cant Get the activity indicator to show on iPhone app
问题描述
我有一个在IB中创建的视图.在其中,我有一个以编程方式创建的滚动视图.在滚动视图中,我连接到Web服务并获取内容和图像.我要在执行此操作时显示活动指示器.所以我有:
I have a view which is created in IB. inside it I have a scroll view created programmatically. Within the scroll view I connect to a web service and fetch the content and the image. I want to show an activity indicator while doing this. So I have:
- (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之后,我有:
And right after the scrollview is added, I have:
[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中,我有:
And in loadimage I have:
- (void)loadImage {
[activityIndicator startAnimating];
我已经尝试过[self.view.window addSubview:activityIndicator];,[self-> ScrollView addSubview:activityIndicator],[self.view addSubview:activityIndicator];
I've tried [self.view.window addSubview:activityIndicator];, [self->ScrollView addSubview:activityIndicator];, [self.view addSubview:activityIndicator];
但是我无法显示该指示器.任何人都知道什么地方可能出问题了吗?
but I just can't get the indicator to show. Anyone have any idea what might be wrong?
推荐答案
您应该在viewDidload中做到这一点
You should do this in you viewDidload
- (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方法将从互联网上获取所有数据并进行其他工作.
Note: "performSelectorInBackground" will show you an activity indicator on the view and your loadScroll method will fetch all data from internet and do other work.
-(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];
}
这篇关于不能获取活动指示器以显示在iPhone应用程序上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!