Modal View Controller displays blank first time, works properly subsequently

社会主义新天地 提交于 2019-12-11 16:38:28

问题


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];

回答1:


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.

You could move the loadRequest call after the presentModalViewController call.

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];

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



来源:https://stackoverflow.com/questions/4472824/modal-view-controller-displays-blank-first-time-works-properly-subsequently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!