简体   繁体   English

无法使UIWebView在除应用程序委托以外的任何视图控制器中工作

[英]Can't get UIWebView working in any view controller other than app delegate

I've been messing around with some open source code trying to figure cocoa out. 我一直在弄乱一些开源代码,试图弄清楚可可。 I've gotten an application with multiple views, and I want to put UIWEBVIEWS on the second, third, and fourth view controllers I've made. 我已经获得了具有多个视图的应用程序,并且我想将UIWEBVIEWS放置在我制作的第二,第三和第四视图控制器上。 However I can't seem to figure it out. 但是我似乎无法弄清楚。 I can actually pull down on the touch screen a black/blank UIWEBVIEW on the fourth view controller when I run it on my iphone. 当我在iPhone上运行它时,实际上可以在触摸屏上的第四个视图控制器上拉黑/空白UIWEBVIEW。 I think it's just missing the following URL code. 我认为它只是缺少以下URL代码。 Where do I need to put it? 我需要放在哪里?

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xbox-scene.com"]]];

Have you looked at UIViews' viewDidLoad: method? 您是否看过UIViews的viewDidLoad:方法? It will be invoked when the view is retrieved from a nib. 从笔尖检索视图时将调用它。 You can place your loadRequest there. 您可以在此处放置loadRequest。

Here's the code in a little better view from my fourthviewcontroller.m: 这是我的fourthviewcontroller.m中的视图中更好的代码:

  • (void)viewDidLoad { (void)viewDidLoad {

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@" http://xbox-scene.com "]]]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@“ http://xbox-scene.com ”]]];

// segmented control action
[myControl addTarget:self 
          action:@selector(loadAnotherView:) 
    forControlEvents:UIControlEventValueChanged];
[super viewDidLoad];

} }

Thats what i thought. 那正是我所想。 Yet it's a no go...I don't think i'm totally off base here but maybe I am. 但这是不行的...我不认为我在这里完全不在基地,但也许是。

The XIB file for the fourth view in the source code provided suggests that you are creating two instances of FourthViewController (one being the File's owner itself). 在提供的源代码中,第四个视图的XIB文件表明您正在创建FourthViewController的两个实例(一个是文件的所有者本身)。

The UIWebview object in the view is connected to the 'webview' property of the second FourthViewController instance to which no view is connected and being loaded. 视图中的UIWebview对象连接到第二个FourthViewController实例的“ webview”属性,该实例没有连接视图并被加载。 So its -viewDidLoad method never gets called and hence the webpage never gets loaded. 因此,它的-viewDidLoad方法永远不会被调用,因此该网页也永远不会被加载。

If you remove the second instance and connect the webview property from the Files owner to the UIWebview object then the code should work fine. 如果删除第二个实例并将webview属性从“文件”所有者连接到UIWebview对象,则代码应该可以正常工作。 It did for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM