我有一个在IB中创建的视图。在里面我有一个以编程方式创建的滚动视图。在滚动视图中,我连接到Web服务并获取内容和图像。我想在执行此操作时显示活动指示器。所以我有:
- (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-> ScrollView addSubview:activityIndicator] ;, [self.view addSubview:activityIndicator];
但我无法得到指标。任何人都知道可能出现什么问题?
答案 0 :(得分:5)
您应该在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方法将从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];
}
答案 1 :(得分:0)
在多次遇到上述活动指标问题后,到目前为止我发现了什么。