无法显示活动指示器iOS

时间:2015-08-24 09:48:53

标签: ios uiwebview activity-indicator

我无法在UIWebView上显示活动微调器 在页面加载时需要显示UIActivityIndi​​catorView。 声明了UIActivityIndi​​catorView,但它从未在UIWebView上显示

请协助。 代码如下:

#import <QuartzCore/QuartzCore.h>
#import "MyScreen"
#import "Reachability.h"

@interface MyScreen ()
@end

@implementation MyScreen
{;
    UIView *loadingView;
}

@synthesize gotoMainMenu;

- (void)viewDidLoad {
    [super viewDidLoad];
    loadingView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
    loadingView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
    loadingView.layer.cornerRadius = 5;

    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.center = CGPointMake(loadingView.frame.size.width / 2.0, 35);
    [activityView startAnimating];
    activityView.tag = 100;
    [loadingView addSubview:activityView];

    UILabel* lblLoading = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, 80, 30)];
    lblLoading.text = @"Loading...";
    lblLoading.textColor = [UIColor whiteColor];
    lblLoading.font = [UIFont fontWithName:lblLoading.font.fontName size:15];
    lblLoading.textAlignment = NSTextAlignmentCenter;
    [loadingView addSubview:lblLoading];

    [self.view addSubview:loadingView];
    // Do any additional setup after loading the view.
}

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

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:true];
    self.navigationController.navigationBar.hidden=false;
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reach currentReachabilityStatus];
    if (netStatus != NotReachable)
    {
        NSLog(@"Network is Available");
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Please check internet connectivity!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil , nil];
        [alertView show];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(id) init
{
    if([super init])
    {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, -64.00, self.view.frame.size.width, self.view.frame.size.height+64.0)];
        webView.scalesPageToFit = YES;
        webView.userInteractionEnabled = YES;
        url = [[NSURL alloc] initWithString:@"http://theURL/"];
        req = [[NSURLRequest alloc] initWithURL:url];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:5.0];
        [webView loadRequest:theRequest];
        [webView loadRequest:req];
        [self.view addSubview:webView];
    }
    return self;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [loadingView setHidden:YES];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [loadingView setHidden:NO];
}

@end

1 个答案:

答案 0 :(得分:0)

您不应在init函数中使用“self.view”。使用它时,将加载视图并在添加webView之前调用viewDidLoad。因此,在加载视图后添加webView。

试着移动这一行:

[self.view addSubview:webView];

后:

[self.view addSubview:loadingView];