Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a activity indicator in middle of view programmatically?

I am working with an app which parses an feed online. When I click the refresh button, it takes some time to re parse the file and show its data. I want an activity indicator in the middle of the view when I click refresh button. And when parsing is done, that indicator should hide.

I am using this code but it is not working:

- (IBAction)refreshFeed:(id)sender
{
   UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
   [self.view addSubview:spinner];

   [spinner startAnimating];

   // parsing code code
    
   [spinner release];
}
like image 359
Piscean Avatar asked Feb 24 '11 10:02

Piscean


People also ask

How do I add activity indicator in swift 5?

Go to the Storyboard and drag two buttons to the main view. Set the title of the buttons to "Start" and "Stop". Also Drag an Activty Indicator View to the main view and position it below the other two buttons, The Storyboard should look like this.


1 Answers

I think i am not able to explain it well.ok lets try again. its a nav based app. i have tableView. every cell creates a detailView. on RootView which is a tableview there is a refresh button. when i click that button it parses the feed again. and it takes some time. for that time program doesnt respond. and parsing is complete it works again. now i need activity indicator for that time. i dont know how to add in xib. bcz when i open main.xib and put activity indicator in RootViewController. it comes infront of whole tableView. now may be i explained well. if not let me know i ll try again.

from what you are saying above the program is not responding during the parsing which is a problem. If the GUI freezes while you are parsing the data you should move that operation to a secondary thread. That way your GUI remains responsive and you will be able to see the activity indicator.

on the main thread you should have something similar to this in order to show the activity indicator:

UIActivityIndicatorView  *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame = CGRectMake(round((yourView.frame.size.width - 25) / 2), round((yourView.frame.size.height - 25) / 2), 25, 25);
av.tag  = 1;
[yourView addSubview:av];
[av startAnimating];

after the secondary thread is finished, this is the thread where you parse the data, you should call on the main thread something like this to remove the activity indicator:

UIActivityIndicatorView *tmpimg = (UIActivityIndicatorView *)[yourView viewWithTag:1];
[tmpimg removeFromSuperview];
like image 190
Sorin Antohi Avatar answered Oct 24 '22 16:10

Sorin Antohi