Xcode 4.5 - OS X Cocoa 应用程序 - 基本 Web 视图:打开时加载 Google
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14474142/
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
Xcode 4.5 - OS X Cocoa Application - Basic Web View: Load Google when opened
提问by user1822824
I'm trying to create an extremely basic OS X Cocoa Application that when opens loads http://www.google.com. As basic as possible (no back or forward buttons, etc.).
我正在尝试创建一个非常基本的 OS X Cocoa 应用程序,它在打开时会加载http://www.google.com。尽可能基本(没有后退或前进按钮等)。
I have little experience with Xcode 4.5 and I can't find any tutorials regarding a web view for OS X Cocoa Application AND Xcode 4.5. I was able to find a tutorial and create a web view for an iOS web view. I took what I learned but didn't get very far.
我对 Xcode 4.5 的经验很少,我找不到任何关于 OS X Cocoa 应用程序和 Xcode 4.5 的 Web 视图的教程。我找到了一个教程并为 iOS Web 视图创建了一个 Web 视图。我接受了我所学到的,但并没有走多远。
Here's what I did so far:
这是我到目前为止所做的:
- Created New OS X Cocoa Application in Xcode 4.5.2
- Added a WebView object to the Window object
- 在 Xcode 4.5.2 中创建新的 OS X Cocoa 应用程序
- 向 Window 对象添加了一个 WebView 对象
Based on the iOS web view tutorial I assume all I need to do is add a couple lines of code and it should work?
基于 iOS 网络视图教程,我假设我需要做的就是添加几行代码,它应该可以工作吗?
This is the code I used in the iOS web view (ViewController.m):
这是我在 iOS 网页视图 (ViewController.m) 中使用的代码:
NSURL *myURL = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
Any help would be much appreciated. I've been stuck on this all night.
任何帮助将非常感激。我整晚都被困在这个问题上。
回答by NSGod
After adding the WebView
to your main window, you'll want to make sure you've added the WebKit.framework
to the Linked Frameworks and Libraries for your project otherwise you'll get a linking error.
将 加入WebView
主窗口后,您需要确保已将 加入WebKit.framework
到项目的链接框架和库中,否则会出现链接错误。
.h:
。H:
@class WebView;
@interface MDAppDelegate : NSObject <NSApplicationDelegate>
@property (weak) IBOutlet WebView *webView;
@property (assign) IBOutlet NSWindow *window;
@end
Assuming you've created an IBOutlet
for the WebView
named webView
like in the code above, you can load a URL using the code below:
假设您已经IBOutlet
为上面代码中的WebView
命名webView
类创建了一个,您可以使用以下代码加载一个 URL:
.m:
.m:
@implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://www.google.com/"]];
[self.webView.mainFrame loadRequest:request];
}
@end
Sample GitHub project: https://github.com/NSGod/WebViewFinagler
示例 GitHub 项目:https: //github.com/NSGod/WebViewFinagler
回答by Ravindra Bagale
add self
keyword to following line
将self
关键字添加到以下行
[myWebView loadRequest:myRequest];
like this
像这样
[self.myWebView loadRequest:myRequest];
try following code,it work for me,just tested
尝试以下代码,它对我有用,刚刚测试
1) create new project
2) select ViewController ,goto xcode menu
Editor->Embed in->Navigation controller
3)declare myWebView property in ViewController.h
1) 创建新项目
2) 选择 ViewController ,转到 xcode 菜单
Editor->Embed in->Navigation controller
3) 在 ViewController.h 中声明 myWebView 属性
in ViewController.m write following code
在 ViewController.m 中编写以下代码
self.myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
self.myWebView.scalesPageToFit = YES;
[self.view addSubview:self.myWebView];
NSURL *myURL = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[self.myWebView loadRequest:myRequest];