Implementing activity indicator - iphone

I am implementing an activity indicator in my application. In my application on button click a webservice is called and it takes some time. To show the user that process is going on I implemented an activity indicator:
CGRect frame = CGRectMake(140, 300, 40, 37);
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:frame];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];
This Snippet is written in viewDidLoad() method, and I have an action called,
-(IBAction)agree:(id)sender
{
//here webservice is called
}
I have to start that activity by [activityIndicator startAnimating];
But I am unable to start that activityIndicator, please suggest a proper solution for that.

Put this code into your web view.
-(void) viewDidLoad
{
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 20.0, 20.0);
activityIndicator.center=self.view.center;
[activityIndicator startAnimating];
[self.view addSubview:activityIndicator];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
Refer this link link text. This explains to display the activity indicator for parsing action of the rss feed
Best of Luck.

Related

UIActivityIndicator in xcode?

I m trying out for the concept of UIActivityIndicator.In the firstView I have a tableView loaded with data and corresponding accessorybutttons.So when a accessorybutton of a tableViewCell is tapped then DetailsView is loaded.Meanwhile Im adding an activityIndicator when accessorybutton is tapped.
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityIndicator.color = [UIColor blackColor];
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
DetailsView *detailView= [[DetailsView alloc] initWithNibName:#"DetailsView" bundle:nil];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UILabel *empid=(UILabel*)[cell viewWithTag:117];
detailView.Id=[empid text];
[self presentModalViewController: detailView animated:NO];
[detailView release];
}
Then in DetailsView based on the empid it received from firstView it loads some data
- (void)viewDidLoad
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityIndicator.color = [UIColor blackColor];
[self.view addSubview:activityIndicator];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
////loads data from service url and parsing is done
[activityIndicator stopAnimating];
}
Until here it is working perfectly..I have a back button in DetailsView and whenever it is pressed presentmodalViewController is dismissed.
-(IBAction)btnBack
{
[self dismissModalViewControllerAnimated:NO];
}
But now the problem is I can see the presentmodalViewController getting dismissed but the activityindicator which started animating when accessorybutton is tapped still animating.So how do I stop that animating activityIndicator when presentView is dismissed and display only the data of prevoius view.
in ViewWillAppear method of first view write below code
[activityIndicator hidesWhenStopped];
tell me whether it is working or not!!!
Happy coding!!!!
Use following code
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
detailView.Id=[empid text];
[activityIndicator stopAnimating];
[self presentModalViewController: detailView animated:NO];
}
or in your viewWillDisappear of FirstView
{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperView];
}
in DetailsView class in viewDidLoad method just add activityIndicator as a subview and start animating like bellow..
- (void)viewDidLoad
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityIndicator.color = [UIColor blackColor];
[self.view addSubview:activityIndicator];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
[activityIndicator startAnimating];
}
And also stopAnimating when your data download complete or fail and also in bellow method add in btnBack method or viewWillDisappear just add this bellow code
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];///Add this line if you want to remove from superview
In the viewWillAppear of first view add [activityIndicator stopAnimating];
-(void)viewWillAppear:(BOOL)animated
{
if (activityIndicator)
{
[activityIndicator stopAnimating];
}
}

Unable to see UIActivityIndicator in UIWebView

I am new to iPhone developer,
I am loading pages in my webview, loading takes too much time so i wan to show ActivityIndicator till the page loads,
Here is my code snippet but it is not working,
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
_webview=[[UIWebView alloc]init];
[_webview setBackgroundColor:[UIColor grayColor]];
[_webview setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:_webview];
[_webview bringSubviewToFront:activityIndicator];
...
- (void)webViewDidStartLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
activityIndicator.hidden=FALSE;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
activityIndicator.hidden=TRUE;
}
but i am unable to see my activityIndicator
Any help will be appreciated.
You are adding the UIActivityIndicatorView directly to the self.view and later you are trying to bringSubviewToFront from the UIWebView. Check with:
[self.view bringSubviewToFront:activityIndicator];
Change [_webview bringSubviewToFront:activityIndicator]; to [self.view bringSubviewToFront:activityIndicator];
You can also change the order in which you add subviews. Add the webview first, and then add the activity indicator:
_webview=[[UIWebView alloc]init];
[_webview setBackgroundColor:[UIColor grayColor]];
[_webview setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:_webview];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);//you should also keep the height and width equal
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
And get rid of the line that follows:[_webview bringSubviewToFront:activityIndicator];

How to add an Indicator to UIImagePickerController

I have an image picker to pick multiple images from the photo library. The picked photos are resized and saved to file in imagePickerController:didFinishPickingMediaWithInfo:. It takes a few seconds.
Could I put an indicator on the view?
I have tried to use addSubview:indicatorView in imagePickerController:didFinishPickingMediaWithInfo: method. But indicatorView doesn't appear. It gets dismissed with the imagePickerView.
You should add the UIActivityIndicatorView at the top of the view hierarchy or it will get dismissed with the UIPickerViewController, unless you use a callback after the resize operation and in the callback you dismiss the UIImagePickerController.
Or you could use a progress HUD like SVProgressHUD.
Hope this helps =)
We did a little chat session and solved this way:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
UIView *primaryImage = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];
primaryImage.backgroundColor = [UIColor redColor];
primaryImage.alpha =0.9;
UIView *secondaryImage = [[UIView alloc] initWithFrame:CGRectMake(0,0,70,70)];
secondaryImage.center = CGPointMake(160, 240);
secondaryImage.backgroundColor = [UIColor blackColor];
secondaryImage.alpha = 0.9;
secondaryImage.layer.cornerRadius = 12;
[primaryImage addSubview:secondaryImage];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
indicator.center = CGPointMake(35, 35);
[indicator setHidesWhenStopped:NO];
[secondaryImage addSubview:indicator];
[indicator startAnimating];
[indicator release];
[secondaryImage release];
[picker.view addSubview:primaryImage];
[primaryImage release];
});
// Put here the code to resize the image
dispatch_async(dispatch_get_main_queue(), ^{
// Dismiss the picker controller
});
});

How to show the activity indicator on tap gesture

I want to show the activity indicator on tap gesture and also navigate from current viewcontroller to next viewcontroller I have written code on tap gesture method my code is -
- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
textLabel.hidden = YES;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
[indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicator];
[indicator startAnimating];
AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
[self.navigationController pushViewController:accountAnfordernViewController animated:YES];
[accountAnfordernViewController release];
}
It navigates to next view controller but indicator are not showing, I want when it navigate to nextviewcontroller at that time indicator also should be show for some time and then navigate to next view controller. What should I do for that?
I suspect you need to do something like this:
- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
textLabel.hidden = YES;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
[indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicator];
[indicator startAnimating];
[self performSelector:#selector(pushAccountAnfordern) withObject:nil afterDelay: 0.1];
}
- (void) pushAccountAnfordern;
{
AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
[self.navigationController pushViewController:accountAnfordernViewController animated:YES];
[accountAnfordernViewController release];
}
You can vary the delay. And, indeed, 0.0 may be just fine. This way of doing it lets the current run loop end, giving a chance for the spinner to start showing before starting the next bit of code. For a different approach (with potential side effects) to forcing the spinner to start before the current run loop ends this this SO question.

UIActivityIndicator not showing up

I have implemented a UIActivityIndicator that shows up in one part of my program but not another. I have the activity indicator come up while i am loading a table, however, i am trying to get it to start animating again after the user has clicked a button and is waiting for the table to reload. The table reloads, but no indicator. Here is the code.
- (void)viewWillAppear:(BOOL)animated {
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
activity = [[UIActivityIndicatorView alloc] initWithFrame: frame];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activity startAnimating];
[navigationUpdateFromDetail.window addSubview: activity];
[super viewWillAppear:animated];
}
It comes up for that part. However, for the next part it does not want to seem to come up.
- (IBAction) btnGreaterTen_clicked :(id)sender {
self.searchDistance = [NSNumber numberWithDouble : 10];
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
activity = [[UIActivityIndicatorView alloc] initWithFrame: frame];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[navigationUpdateFromDetail.window addSubview: activity];
[activity startAnimating];
NSLog(#" search value after change %#", [searchDistance description]);
[self getSetDisplay];
[activity stopAnimating];
}
That button changes a variable and is suppose to start the animation, but it does not. I have changed the color to make sure it was not just blending in, so that is not the solution. I tried to recreate the same object, but still no luck.
Thank you in advance.
That won't work, because the animation will only show when the main application loop is "running". In your code, you're blocking the main thread by calling [self getSetDisplay].
You should load your data asynchronously to make this work (in a background thread). Then you can call startAnimating, start your thread, and when the thread finishes, stop the animation.
This has been bugging me for ages, and I just found that when using a search display controller I had to add the activity indicator as a subview of the search results to get it to show. In my viewDidLoad, I put...
// create activity indicator
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.searchDisplayController.searchResultsTableView addSubview:activityIndicator];
When I want to start animating, i put...
[activityIndicator setCenter:CGPointMake(self.searchDisplayController.searchResultsTableView.frame.size.width/2.0f, self.searchDisplayController.searchResultsTableView.frame.size.height/2.0f)];
[activityIndicator startAnimating];
Hope this helps someone.
try adding
activity.hidden = NO;
just before
[activity startAnimating];