多次调用webViewDidStartLoad方法

时间:2011-11-02 13:55:05

标签: iphone web

我做了以下事情:

头文件

 UIWebView *itemWebView;

实施文件

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    itemWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    itemWebView.delegate = self;

    NSURL *url = [NSURL URLWithString:targetString];
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];

    [itemWebView loadRequest:requestObject];
    [self.view addSubview:itemWebView];
    [itemWebView release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL) webView:(UIWebView *)_webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"ItemLoadinglViewController - shouldStartLoadWithRequest");

    loadingView.hidden = NO;
    itemWebView.hidden = YES;
    return YES;
}

- (void) webViewDidStartLoad: (UIWebView *) _webView {
    NSLog(@"ItemLoadinglViewController - webViewDidStartLoad");


}

- (void) webViewDidFinishLoad: (UIWebView *) _webView { 
    NSLog(@"ItemLoadinglViewController - webViewDidFinishLoad");

    loadingView.hidden = YES;
    itemWebView.hidden = NO;

}

这就是我NSlog的样子:

ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - webViewDidStartLoad
ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - webViewDidStartLoad
ItemLoadinglViewController - webViewDidFinishLoad
ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - webViewDidStartLoad
ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - webViewDidStartLoad
ItemLoadinglViewController - webViewDidFinishLoad
ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - webViewDidStartLoad
ItemLoadinglViewController - webViewDidFinishLoad
ItemLoadinglViewController - shouldStartLoadWithRequest
ItemLoadinglViewController - webViewDidStartLoad
ItemLoadinglViewController - webViewDidFinishLoad

有谁知道为什么这些方法被多次调用?我以前用过这个,但是每个方法只被调用一次。为什么这次我不能弄清楚。谢谢:)

编辑:

- (void) itemPressed:(id)sender{
    int x = [sender tag];

    ItemLoadingViewController *itemLoadingViewController = [[ItemLoadingViewController alloc] init];
    itemLoadingViewController.targetString = [target objectAtIndex:x];
    [self.navigationController pushViewController:itemLoadingViewController animated:YES];
    [itemLoadingViewController release];
}

1 个答案:

答案 0 :(得分:1)

将初始化代码从viewDidLoad移动到init&校验。