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];
}
}
Related
I have a FirstView loaded with tableView and when a accessory button of tableviewCell is tapped then it goes to SecondView.The data in SecondView is loaded from service url and so it takes time.So I need to show activityIndicator when accessorybutton is tapped, stop it after loading the whole data .BUt I couldnot see the activityindicator getting displayed.
This is the code Im working on :
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.color = [UIColor redColor];
[scrollView addSubView:activityIndicator];
[activityIndicator startAnimating];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
[activityIndicator startAnimating];
SecondView *secView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
secView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *name1 = (UILabel*)[cell viewWithTag:111];
secView.provName = [name1 text];
[self presentModalViewController: secView animated:NO];
[secView release];
}
Where I m going wrong?
EDIT:
SecondView:
-(void)viewWillAppear:(BOOL)animated
{
activityIndictr = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndictr.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
activityIndictr.color = [UIColor redColor];
[testscroll addSubview:activityIndictr];
[activityIndictr startAnimating];
}
-(void)viewDidLoad
{
[super viewDidLoad]
// I m calling many service urls(10) and assigning to labels and tableview
[activityIndictr stopAnimating];
}
You should call the activity indicator in the SecondView's ViewWillAppear , so that immediately after pushing in this view the activity indicator starts and then dismiss it when all the data is parsed from the url.
This is because the Second View Controller will be pushed almost immediately and the data will take time to load
You allocate two activity indicators, throw the first one away, then start animating the second one twice and never add any activity indicator into any existing view. No wonder it’s not showing up. Try [[secView view] addSubview:activityIndicator] or something similar.
Do this in viewWillAppear of SecondView :
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
Again I have updated the answer.
put this code in appdelegate And define static object in appdelegate like below
static UIActivityIndicatorView *activityView;
+(void)showActivityIndicator{
if (!activityView) {
activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(160, 230);
[activityView startAnimating];
UIWindow *win = [UIApplication sharedApplication].keyWindow;
[win addSubview:activityView];
}
}
+(void)removeActivityIndicator{
if ([activityView superview]) {
[activityView removeFromSuperview];
activityView = nil;
}
}
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//global static activity indicator... you can access from any other view controlller
[AppDelegate showActivityIndicator];
}
And after getting response
[AppDelegate removeActivityIndicator];
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];
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.
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.
All of the examples I've seen on here and other sites involved creating a UIActivityIndicatorView and loading it with something like:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:myActivityIndicatorView
target:nil
action:nil]
autorelease];
However, that just creates a plain activity indicator in the navigation bar. What I want to do is have a button that looks just like the normal UIBarButtonSystemItem buttons but with an activity indicator instead of one of the default images. I've tried doing initWithImage and initWithTitle with nil images or titles and then adding the activity indicator as a subview, but that doesn't work.
Any ideas?
My Solution is to create a subclass of UIButton:
in SOActivityButton.h:
#interface SOActivityButton : UIButton
{
UIActivityIndicatorView* activityIndicator;
}
#end
in SOActivityButton.m:
#implementation SOActivityButton
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
CGRect innerFrame = CGRectInset(frame, 8.0f, 8.0f);
activityIndicator = [[UIActivityIndicatorView alloc]
initWithFrame:innerFrame];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[self addSubview:activityIndicator];
}
return self;
}
- (void)dealloc
{
[activityIndicator release], activityIndicator = nil;
[super dealloc];
}
- (void) startAnimating
{
[activityIndicator startAnimating];
}
- (void) stopAnimating
{
[activityIndicator stopAnimating];
}
#end
Then to use it:
SOActivityButton* activityButton = [[SOActivityButton alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityButton setImage:[UIImage imageNamed:#"button-background.png"]
forState:UIControlStateNormal];
[activityButton addTarget:self action:#selector(myAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *activityBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:activityButton];
[activityButton release];
self.navigationItem.rightBarButtonItem = activityBarButtonItem;
[activityBarButtonItem release];
You will need to find or create a button-background.png. The PSD here should have one.
have you tried creating a UIButton in the button bar and then adding an activity indicator as a subView of the UIButton?
I have this working and it is very simple. Just place the activity indicator where you want it with IB, but make sure it's lower in the list than the bar you want it on, and is at the "top level" (not a subview of anything else). Then control it in code via an outlet.
Here's something that can help:
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[activityIndicator release];
self.navigationItem.rightBarButtonItem = activityItem;
[activityItem release];
[activityIndicator startAnimating];