2011-12-28 49 views
0

它保持抛出错误:接收器类型webFrame,例如消息是线上的前向声明“[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]]] ;”Cocoa中的Webview无法加载

我.h文件中

@interface AttendanceWizardAppDelegate : NSObject <NSApplicationDelegate> 
{ 
@private WebView *webView; 

} 
@property (weak) IBOutlet WebView *webView; 
@property (assign) IBOutlet NSWindow *window; 

@end 

我.m文件

#import "AttendanceWizardAppDelegate.h" 

@implementation AttendanceWizardAppDelegate 


@synthesize Username = _Username; 
@synthesize Password = _Password; 
@synthesize webView = _webView; 
@synthesize webber = _webber; 
@synthesize window = _window; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
NSString *urlStr = @"www.google.com"; 
[[webView mainFrame ] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]]; 
} 

@end 

回答

2

你只需要添加的WebKit头的进口在你的头文件:

#import <WebKit/WebKit.h> 

您的代码也可以通过不为您声明的属性定义实例变量来简化:

头文件(.H):

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

@interface AttendanceWizardAppDelegate : NSObject <NSApplicationDelegate> 
// No need for a iVar declaration 

@property (weak) IBOutlet WebView *webView; 
@property (assign) IBOutlet NSWindow *window; 

@end 

实现文件(.M):

#import "AttendanceWizardAppDelegate.h" 

@implementation AttendanceWizardAppDelegate 

// Simplified synthesize declarations (no reference to the iVar) 
@synthesize Username; // I suggest you change the name of this variable ; the convention is to start the name of your property with a lower case letter, to not confuse it with a class name 
@synthesize Password; 
@synthesize webView; 
@synthesize webber; 
@synthesize window; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSString *urlString = @"www.google.com"; 

    [[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; // Note the use of self.webView, to use the getter you created by declaring the property 
} 

@end 
+0

进口在那里。 – Kevrone 2011-12-28 15:42:52

+0

编译时或运行时抛出错误吗? – Guillaume 2011-12-28 15:44:10

+0

没关系我在我的Webkit.h中犯了一个错字 – Kevrone 2011-12-28 15:52:30