在Web视图iOS中加载html文件时屏幕变黑

时间:2014-02-03 09:57:54

标签: ios iphone objective-c

我正在尝试在Web视图中加载html文件,并且屏幕变黑。 视图控制器的层次结构是: 图

  

网络视图   导航项目   这是选项卡,有导航控制器来显示视图控制器。

代码:

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webView = [[UIWebView alloc] init];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[JLTBackgroundHelper getBackgroundImageForLandingView]];
    self.webView.opaque = NO;
    self.webView.backgroundColor = [UIColor clearColor];

    UIViewController *rootVC = [[UIViewController alloc] init];
    rootVC.title = @"Contact JLT Sport";
    rootVC.view = self.webView;
    self.viewControllers = @[rootVC];

    NSString *fileName;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        // iPad-specific
        fileName = @"contact_ipad";
    } else {
        // iPhone-specific
        fileName = @"contact";
    }

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html" inDirectory:@"Assets/web"];

    NSURL *url = [NSURL fileURLWithPath:htmlFile];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    _webView.delegate= self;

}


- (BOOL) webView:(UIWebView*)wv shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navType
{
    if (navType == UIWebViewNavigationTypeLinkClicked
        && [request.URL.scheme hasPrefix:@"http"])
    {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }
    return YES;
}

不确定哪里出错了。尝试了超过5个小时,但没有运气。

1 个答案:

答案 0 :(得分:1)

而不是

self.viewControllers = @[rootVC];

试试这个

[self presentViewController:rootVC animated:NO completion:nil];

如果在viewDidLoad中显示控制器会出现问题,请在viewDidAppear中执行。首先,我不清楚为什么要在控制器的viewDidLoad内创建另一个视图控制器并将子视图添加到该控制器中。