ios 在 Xcode 4.2 的 UIWebView 中加载网页——如何正确连接 IBOutlets 并确保网页加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8157144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading web page in UIWebView in Xcode 4.2 — how to connect IBOutlets properly and ensure the web page loads?
提问by Crashalot
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/
大多数关于在 iPhone 应用程序中嵌入 UIWebView 的教程都基于旧版本的 Xcode。这是一个例子:http: //howtomakeiphoneapps.com/uiwebview-tutorial/239/
We followed the steps in that tutorial, but the steps don't quite translate to Xcode 4.2
我们遵循了该教程中的步骤,但这些步骤并没有完全转化为 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?
另一个问题:如何将 UIWebView 链接到 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.
当我们添加 UIWebView 并将其连接到 ViewController 时,我们看到的只是一个白屏。网页永远不会加载。
Could anyone share tips on loading a web page with UIWebView for Xcode 4.2?
任何人都可以分享有关使用 Xcode 4.2 的 UIWebView 加载网页的提示吗?
采纳答案by Tommy Herbert
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.
如果您使用的是故事板,文件所有者仍然存在,但它被称为视图控制器。因此,要将故事板中的 UIWebView 链接到 UIWebView 插座,请按住控件,然后单击并将一条线从视图控制器拖到 Web 视图。这一切都在故事板左侧的“视图控制器场景”面板中。
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.
顺便说一下,“使用故事板”下的另一个重要复选框是“使用自动引用计数”。这是一个很棒的功能,但是如果您在学习链接到的教程时打开了它,则需要跳过他释放webView
实例变量的部分。
回答by Alix
.h file
.h 文件
@interface webViewViewController : UIViewController <UIWebViewDelegate>
.m file
.m 文件
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.
webview 是我为 UIWebView 创建的 IBOutlet 的名称。要制作和出口,只需 Contrl + 从 UIWebView 拖放到 @interface 和 @end 之间的 H 文件。
Hope this will help you.
希望这会帮助你。
回答by Evan
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. P.S. Use storyboards.
尝试为 Web 视图代码创建一个新类。要创建一个新类,请在 Xcode 中右键单击或控制+单击左侧栏中的项目文件夹。选择“New File”,创建一个新的类,它是 UIViewController 的子类。取消选中这两个复选框。然后,选择将包含 Web 视图的屏幕并选择右侧栏中的身份检查器。将班级更改为您之前创建的班级的名称。使用您找到的教程中的所有代码,但 -(void)dealloc 部分除外。将所有插座连接到 UI 元素,您应该就完成了。如果您仍然遇到问题,请回复此问题,我很乐意提供帮助。PS 使用故事板。