iOS中的活动指示器的行为不一致

时间:2013-12-12 15:37:15

标签: ios webview uiactivityindicatorview

在我的应用程序中,我有一些viewcontrollers包含带有活动指标的webview,它们运行正常。现在,在一个带有webview和活动指示器的新视图控制器中,我得到了一些奇怪的行为。在模拟器中,视图会加载,但即使视图加载完毕,活动指示器仍会保持旋转状态。我从另一个具有webview和活动指示器的视图控制器中完全复制了所有内容,但是奇怪的行为仍然是他们的。这些URL只是常规的小型html文件,所以它不会像任何一个需要很长时间才能加载。我不知道这是否是xcode(ver 5.0)中的一个小故障,或者很可能是我在代码中忽略的东西。工作视图控制器的代码如下:

#import "ProgramPDFViewController.h"

@interface ProgramPDFViewController ()

@end

@implementation ProgramPDFViewController
@synthesize webView;
@synthesize activity;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [TestFlight passCheckpoint:@"PDFProgram-info-viewed"];

    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" "    style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButtonItem;

    // Do any additional setup after loading the view.

    NSString *httpSource = @"<myURL>";
    NSURL *fullUrl = [NSURL URLWithString:httpSource];
    NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
    [webView loadRequest:httpRequest];

}

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

-(void)webViewDidStartLoad:(UIWebView *)WebView
{
    [activity startAnimating];


}

-(void)webViewDidFinishLoad:(UIWebView *)WebView
{
    [activity stopAnimating];
    activity.hidden = TRUE;

}

@end

具有奇怪行为的代码如下:

#import "ComitteeMeetingsViewController.h"

@interface ComitteeMeetingsViewController ()

@end

@implementation ComitteeMeetingsViewController
@synthesize webView, activity;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [TestFlight passCheckpoint:@"CommitteMeetings-info-viewed"];

    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButtonItem;


    NSString *httpSource = @"<myOtherURL>";
    NSURL *fullUrl = [NSURL URLWithString:httpSource];
    NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
    [webView loadRequest:httpRequest];
}

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

-(void)webViewDidStartLoad:(UIWebView *)WebView
{
    [activity startAnimating];


}

-(void)webViewDidFinishLoad:(UIWebView *)WebView
{
    [activity stopAnimating];
    activity.hidden = TRUE;
}

@end

1 个答案:

答案 0 :(得分:0)

你确定这些方法实际上被调用了吗? 您是否设置了webView的委托并在头文件中包含了UIWebViewDelegate?

进行问题排查:尝试添加

NSLog(@"webViewDidStartLoad");

NSLog(@"webViewDidFinishLoad"); 

到相应的方法,看看它们是否出现在日志中