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. However I can't seem to figure it out. I can actually pull down o开发者_JS百科n the touch screen a black/blank UIWEBVIEW on the fourth view controller when I run it on my iphone. I think it's just missing the following URL code. Where do I need to put it?
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xbox-scene.com"]]];
Have you looked at UIViews' viewDidLoad: method? It will be invoked when the view is retrieved from a nib. You can place your loadRequest there.
Here's the code in a little better view from my fourthviewcontroller.m:
- (void)viewDidLoad {
[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).
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. So its -viewDidLoad method never gets called and hence the webpage never gets loaded.
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. It did for me.
精彩评论