iOS删除所有活动指标

时间:2011-09-22 14:24:02

标签: iphone ios

我的活动指标存在问题。我有一个按钮,可以在活动指示器之间重新加载网站和显示。问题是如果用户在按钮上击中超过1次,它将重建一个新的指示器,并且该指示器一直在屏幕上冻结。 按钮禁用无效。有没有人解决这个问题。请帮忙。

这是我的代码:

-(IBAction) buttonReload {
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];    
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;
 }


}

1 个答案:

答案 0 :(得分:0)

一旦页面加载,您应该执行stopAnimatingremoveFromSuperView。如果用户再次刷新页面,您可以再次将其添加到subViewstartAnimating