iOS remove all Activity Indicators
i have a Problem with my Activity Indicators. I have an Button which reload a Website and Display between a Activity Indicator. The Problem is if the User hits more than 1 Times on the Button it will rebuild a new Indicator and this Indicator freezes on the Screen all the Time. Button disabling is not working. Has anyone a Solution for this Problem. Please help.
Here is my Code:
-(IBAction) buttonReload {
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabi开发者_运维技巧lityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)){
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:@"No Inet!" message:@"You need a Inet Connection..."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
else
{
//Website loading
[self performSelector: @selector(doLoadWebsite)
withObject: nil
afterDelay: 0];
return;
}
}
- (void) doLoadWebsite
{
//add activity indicator
NewsActivity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140.0f, 180.0f, 40.0f, 40.0f)];
[NewsActivity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview: NewsActivity];
[NewsActivity startAnimating];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(finishActivity) userInfo:nil repeats:YES];
//NewsActivity.backgroundColor = [UIColor grayColor];
NewsActivity.hidesWhenStopped = YES;
// Show Status Bar network indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//perform time-consuming tasks
//load News Website
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
}
-(void) finishActivity {
if (!webView.loading) {
[self.NewsActivity removeFromSuperview];
[NewsActivity stopAnimating];
//Hide network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
else {
[NewsActivity startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
}
You should perform stopAnimating
, removeFromSuperView
once the page is loaded. If the user refreshes the page again, you can again add it to the subView
and startAnimating
.
精彩评论