2013-04-10 31 views
2

我刚开始开发Mac应用程序,我想了一个网页视图URL时,应用程序start.Here是我的代码:如何在WebView(OSX项目)中启动时加载URL?

AppDelegate.h

#import <Cocoa/Cocoa.h> 
#import <WebKit/WebKit.h> 

@interface AppDelegate : NSObject <NSApplicationDelegate> { 
    WebView *myWebView; 
    //other instance variables 
} 

@property 

(retain, nonatomic) IBOutlet WebView *myWebView; 

//other properties and methods 

@end 

AppDelegate.m

#import "AppDelegate.h" 
#import <WebKit/WebKit.h> 

@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSString *urlText = @"http://google.com"; 
    [[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]]; 
    return; 
    // Insert code here to initialize your application 
} 

@end 

如何在启动时在WebView(OSX项目)中加载URL?

我认为代码可以工作,但是当我尝试在Interface Builder中使用WebView连接代码时,我无法在插座列表中找到“网络视图”。 谢谢 我在更新我的代码后的最后一篇文章,但仍然无法正常工作。 再次感谢您的回复。

+0

do @synthesize myWebView; ?? – iPatel 2013-04-10 09:32:42

+0

@iPatel与最新的LLVM不再需要'@ synthesize'。 – rckoenes 2013-04-10 09:33:29

+0

感谢您的回复!现在我可以从应用程序委托菜单中定义出口“Web视图”。但仍然无效:Web视图不加载URL。 – Skynext 2013-04-10 09:44:58

回答

6

很难确定是什么问题在这里,所以猜测...

哪种方式,你拖在IB的连接?

要连接插座要从中检查显示的Web视图出口拖累:

making the connection

如果在其他方式拖动,从web视图的应用程序委派您尝试连接操作的大纲。

你也有一个问题,在你的代码,你的实例变量:

@interface AppDelegate : NSObject <NSApplicationDelegate> 
{ 
    WebView *myWebView; 
    //other instance variables 
} 

不会被你的财产使用:

@property (retain, nonatomic) IBOutlet WebView *myWebView; 

为您的属性会自动合成并因此将创建一个实例变量_myWebView。你应该看到一个编译器警告这个效果。

这又意味着声明:

[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]]; 

不会做你所期望的,因为myWebViewnil,而不是指你的WebView。你应该是指财产self.myWebView

[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]]; 

随着你应该看到谷歌在Web视图这些变化。

HTH

+0

Finality它的工作非常感谢你! – Skynext 2013-04-10 10:51:58

7

您需要添加WebKit框架。 enter image description here

#import <WebKit/WebKit.h> 

enter image description here

+1

IBOutlets应该弱 – pshah 2015-04-28 17:37:12

+0

检查http://stackoverflow.com/a/7729141/400552 – pshah 2015-04-30 03:13:23

+0

@pshah:iOS vs OSX ...在OSX中你很少关闭窗口,它们仍然被加载并保留在内存中,与你在视图和viewControllers的每个弹出窗口上释放和释放的iOS相比。是否有意义? – 2015-04-30 08:06:23

相关问题