I am trying to show a activity indicatorvw when user taps on the cell of the table view it should show and as soon as detail view loaded it should be hidden.
But it is showing after view loaded and then hides. so it is useless to show indicator while view loads
Inside viewdidload:
activityBgImageView = [[UIView alloc] initWithFrame:self.view.frame];
[activityBgImageView setBackgroundColor:[UIColor blackColor]];
[activityBgImageView setAlpha:0.7];
[activityBgImageView setCenter:self.view.center];
[activityBgImageView setHidden:YES];
activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicatorView setCenter:CGPointMake(self.view.center.x+15, self.view.center.y-15)];
[activityIndicatorView setHidesWhenStopped:YES];
[activityBgImageView addSubview:activityIndicatorView];
[self.view addSubview:activityBgImageView];
Here is my code for table did select:
if([arrPostsData count]>0)
{
[self startActivityIndicatorView];
[self performSelector:@selector(showDetailVw) withObject:nil];
}
-(void)showDetailVw
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
detailVwBg.alpha = 0.0;
detailVwBg.alpha = 1.0;
btnBack.alpha = 1;
[self showPostDetail:arrPostsData tapedCell:detailvwScrollCount PostedFiles:arrUserPostedFilesDataDic comments:arrCommentsDataDic];
isDetailvwActive = TRUE;
[UIView commitAnimations];
}
and inside showPostDetail: method end i stop the indicator
Please suggest where I need to put correct code
these are indicator methods:
#pragma mark processindicator methods
-(void)startActivityIndicatorView{
[activityBgImageView setHidden:NO];
[activityIndicatorView startAnimating];
[self.view bringSubviewToFront:activityBgImageView];
if (![[UIApplication sharedApplication] isIgnoringInteractionEvents])
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
}
}
-(void)stopActivityIndicatorView{
if ([[UIApplication sharedApplication] isIgnoringInteractionEvents])
{
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
[activityBgImageView setHidden:YES];
[activityIndicatorView stopAnimating];
}
A little bit of background about threads & iOS. For all UI elements to change (visually) the programmer needs to make changes to them in the main thread.
Since
activityIndicatorViewis a UI element, it has to run on main thread for it to display the spinning. If you are doing something else on your main thread thenactivityIndicatorViewis blocked. Hence you are not seeing the spinning instantaneously. You will see it when the thread is freed up.If you want this to work then first start the spinner, then background thread, do processing in the bg thread, after it completes, come to main thread, stop spinner, and finally update the UI with the changes.
Hope this helps…