Cocoa中的Webview无法加载

时间:2011-12-28 14:26:50

标签: xcode cocoa webview

它保持投掷错误:接收器类型webFrame例如消息是行上的前向声明“[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];”

我的.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

1 个答案:

答案 0 :(得分: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