【问题标题】:webView loading not being called未调用 webView 加载
【发布时间】:2011-12-18 03:17:43
【问题描述】:

我正在通过按下表格视图单元格来加载页面。我放了一个 if 语句:

       if ([webViewController webView].loading == YES) {

    NSLog(@"Its loading!");

    if (act == nil) {
        NSLog(@"Its nil");

act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        act.color = [UIColor blackColor];

    } else {

        act.hidden = NO;
    }

    act.center = self.view.center;
    [act startAnimating];


} else {

    [act stopAnimating];
    [act setHidden: YES];
}

但是 NSLog 永远不会出现,所以它永远不会被调用。但是当我将它部署到我的 iPhone 时,它​​需要很长时间才能加载,但它仍然没有被调用。这里发生了什么?

更新 - 环绕代码

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"TOUCHED CELL!");

// Push the web view controller onto the navigation stack - this implicitly 
// creates the web view controller's view the first time through
[[self navigationController] pushViewController:webViewController animated:YES];

// Grab the selected item
RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]];

// Construct a URL with the link string of the item
NSURL *url = [NSURL URLWithString:[entry link]];

// Construct a request object with that URL
NSURLRequest *req = [NSURLRequest requestWithURL:url];



// Load the request into the web view 
[[webViewController webView] loadRequest:req];

if ([webViewController webView].loading == YES) {

    NSLog(@"Its loading!");

    if (act == nil) {
        NSLog(@"Its nil");

act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        act.color = [UIColor blackColor];
        act.center = self.view.center;
        [self.view addSubview:act];

    } else {

        act.hidden = NO;
    }

    act.center = self.view.center;
    [act startAnimating];


} else {

    [act stopAnimating];
    [act setHidden: YES];
}

webViewController = nil;
webViewController = [[WebViewController alloc] init];

  }

【问题讨论】:

  • 上下文很重要。这段代码在什么功能中?它周围有什么代码?
  • 在uitableview委托协议方法中:
  • -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

标签: objective-c xcode uitableview uiwebview uiactivityindicatorview


【解决方案1】:

我不确定你想在这里做什么。如果您想在加载 Web 视图时显示活动指示器,您应该将该代码放在 webViewController 中。 (无论如何,在您推送 Web 视图控制器后,您的代码所在的视图控制器将不可见。)

确保 webViewController 是 webView 的委托,然后覆盖 webViewDidStartLoad: 和 webViewDidFinishLoad: (和 webView:didFailLoadWithError:) 以显示和隐藏您的活动指示器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2017-03-29
    • 2011-11-16
    相关资源
    最近更新 更多