简体   繁体   中英

Loading web page in UIWebView in Xcode 4.2 — how to connect IBOutlets properly and ensure the web page loads?

Most tutorials on embedding a UIWebView in an iPhone app are based on older versions of Xcode. Here's an example: http://howtomakeiphoneapps.com/uiwebview-tutorial/239/

We followed the steps in that tutorial, but the steps don't quite translate to Xcode 4.2

There is no concept of a File Owner, for instance, but there is a "storyboard."

Another question: how to link the UIWebView to the UIWebView IBOutlet?

When we add the UIWebView and connect it to the ViewController, all we see is a white screen. The web page never loads.

Could anyone share tips on loading a web page with UIWebView for Xcode 4.2?

If you're using a storyboard, the file owner is still there but it's called View Controller. So to link the UIWebView in the storyboard to the UIWebView outlet, you hold down control, then click and drag a line from the View Controller to the Web View. This is all in the 'View Controller Scene' panel to the left of the storyboard.

Note that when you first create your project from the Single View Application template, there's no need to leave the 'Use Storyboard' checkbox ticked. You might find it easier to follow these older tutorials if you don't use a storyboard.

By the way, another important checkbox, just under 'Use Storyboard', is 'Use Automatic Reference Counting'. This is a great feature, but if you have it turned on while you're following the tutorial you've linked to, you'll need to skip the part where he releases the webView instance variable.

.h file

@interface webViewViewController : UIViewController <UIWebViewDelegate>

.m file

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 640.0)];
NSURL *URL = [NSURL URLWithString:@"http://google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
webView.delegate = self ;
[webView loadRequest:requestObj];
[self.view addSubview:webView];

webview is name of IBOutlet that I have created for UIWebView. To make and outlet, just Contrl + drag and drop from UIWebView to your H file between @interface and @ end.

Hope this will help you.

Try making a new class just for the web view code. To make a new class, right click or control+click on your project folder in the left bar while in Xcode. Choose "New File", and make a new class that is a subclass of UIViewController. Leave both checkboxes unchecked. Then, select the screen that will have the Web View in it and select the Identity inspector in the bar on the right. Change the class to the name of the class you made earlier. Use all of the code from the tutorial you found, except for the -(void)dealloc part. Connect all outlets to UI elements and you should be done. Please reply to this if you are still having problems, I'd be glad to help. PS Use storyboards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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