模态视图控制器第一次显示空白,随后正常工作 [英] Modal View Controller displays blank first time, works properly subsequently

查看:83
本文介绍了模态视图控制器第一次显示空白,随后正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模态视图控制器,我试图在其中呈现一个Web视图。它第一次出现时,显示为空白。当我将其关闭并再次单击时,它显示正常。我认为这可能是加载webView的一个问题,但我只是在webView完成加载时才尝试显示它,但它永远不会被调用。

I have a modal view controller that I am trying to present a web view in it. Tthe first time it appears, it shows up as blank. When I close it out and click it again, however, it displays fine. I'm thinking it may be an issue with the loading of the webView, but I've tried displaying it only when the webView finishes loading, but it never gets called then.

NSURL* newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webViewController.webView loadRequest: newURLRequest];
[newURL release];
[newURLRequest release];
webViewController.modalPresentationStyle=UIModalPresentationPageSheet;
[self presentModalViewController:webViewController animated:YES];


推荐答案

第一次无法访问webView控件直到第一个当前调用导致控件被加载和初始化。在第一个出现之前,webViewController.webView将为nil,因此在其上调用loadRequest将不执行任何操作。

The webView control won't be accessible the first time until the first present call causes controls to be loaded and initialized. Before the first present, webViewController.webView will be nil and so calling loadRequest on it will do nothing.

您可以在presentModalViewController调用之后移动loadRequest调用。

You could move the loadRequest call after the presentModalViewController call.

但是不是直接在视图控制器的视图中访问控件,最好在WebViewController中将url字符串声明为NSString属性(称为urlString),并在之前将其设置为fileString presentModalViewController调用(并且不要在那里创建NSURL等):

But instead of accessing controls in view controller's views directly, it'd be better to declare the url string as a NSString property (called say urlString) in WebViewController and set it to fileString before the presentModalViewController call (and don't create the NSURL, etc there):

webViewController.urlString = fileString;
[self presentModalViewController:webViewController animated:YES];

最后,在 WebViewController 中,在 viewWillAppear: viewDidAppear:,创建NSURL(使用urlString),创建NSURLRequest,并调用loadRequest。

Finally, in WebViewController, in viewWillAppear: or viewDidAppear:, create the NSURL (using urlString), create the NSURLRequest, and call loadRequest.

这篇关于模态视图控制器第一次显示空白,随后正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆