当webView完成加载时,活动指示器不会消失

时间:2014-07-21 17:25:47

标签: ios objective-c

我正在使用Objective-c开发iOS App。然后,我正在实现自定义的活动指示器,该指示器在加载webView时处于活动状态。

当webView开始加载时会出现自定义的活动指示器,但是当webView完成加载时它不会消失。

我的代码正在关注。

你能告诉我如何解决这个问题吗?

@interface DetailViewController ()<UIWebViewDelegate>{
{
UILabel *loadingLabel;
UIActivityIndicatorView *loadingMark;
UIView *loadingContainer;
}
@property (weak, nonatomic) IBOutlet UIWebView *detailPage;
@end

@implementation DetailViewController

- (void)configureView
{
    NSURL *page_url = [NSURL URLWithString:@"http://yahoo.co.jp"];
    NSURLRequest *request = [NSURLRequest requestWithURL:page_url];
    [_detailPage loadRequest:request];
    _detailPage.delegate = self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)webViewDidStartLoad:(UIWebView*)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self animatingIndicator];
}


- (void)webViewDidFinishLoad:(UIWebView*)webView
{
[self stopAndRemoveIndicator];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  
}

-(void)animatingIndicator {

loadingContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 60)];

//Set Style Of Label
loadingLabel = [[UILabel alloc] init];
loadingLabel.text = @"Loading";
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.shadowColor = [UIColor blackColor];
loadingLabel.shadowOffset = CGSizeMake(1, 1);
loadingLabel.font = [UIFont boldSystemFontOfSize:17];
loadingLabel.backgroundColor = [UIColor lightGrayColor];
loadingLabel.frame = CGRectMake(20, 17, 70, 25);
[loadingContainer addSubview:loadingLabel];

//Set Style Of Mark
loadingMark = [[UIActivityIndicatorView alloc]    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
loadingMark.frame = CGRectMake(100, 14, 30, 30);
[loadingContainer addSubview:loadingMark];

//Set Style Of Container
loadingContainer.backgroundColor = [UIColor lightGrayColor];
[[loadingContainer layer] setCornerRadius:8.0f];
[[loadingContainer layer] setMasksToBounds:YES];
[[loadingContainer layer] setBorderWidth:0.5f];
[[loadingContainer layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
CGRect rect = self.view.frame;
loadingContainer.center = CGPointMake(rect.size.width/2, rect.size.height/2 * 0.8);
[self.view addSubview:loadingContainer];

[loadingMark startAnimating]; 
loadingContainer.hidden = NO; 
}

-(void)stopAndRemoveIndicator {
[loadingMark stopAnimating];
loadingContainer.hidden = YES;
[loadingMark setHidden:YES];
}

2 个答案:

答案 0 :(得分:2)

您需要在主线程中隐藏UIKit对象。

override func webViewDidFinishLoad(webView:UIWebView)
{
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
      self.loadingSign.stopAnimating()
      self.loadingSign.hidden = true
    }
} 

更新swift 4

override func webViewDidFinishLoad(_ webView: UIWebView)
{
    DispatchQueue.main.async {
      self.loadingSign.stopAnimating()
      self.loadingSign.hidden = true
    }
}

答案 1 :(得分:0)

loadingSign是UIActivity指标。 这就是我所做的,工作正常!

.h文件 @property(保留,非原子)IBOutlet UIActivityIndi​​catorView * loadingSign;

.m文件

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.loadingSign startAnimating];
    self.loadingSign.hidden = NO;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.loadingSign stopAnimating];
    self.loadingSign.hidden = YES;
}