【问题标题】:Webview in Cocoa doesn't loadCocoa 中的 Webview 无法加载
【发布时间】:2011-12-28 14:26:49
【问题描述】:

它不断抛出错误:接收器类型 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

【问题讨论】:

    标签: xcode cocoa webview


    【解决方案1】:

    你只需要在你的头文件中添加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
    

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多