Cocoa的WebView在加载应用程序时不会加载URL

Sau*_*ius 1 xcode cocoa

我正在创建一个简单的应用程序,加载一个URL,我有以下代码

webberAppDelegate.h

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface webberAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    WebView *webber;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet WebView *webber;
@end
Run Code Online (Sandbox Code Playgroud)

webberAppDelegate.m

#import "webberAppDelegate.h"

@implementation webberAppDelegate

@synthesize window;
@synthesize webber;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSString *urlString = @"http://www.apple.com";
    // Insert code here to initialize your application 
    [[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}

@end
Run Code Online (Sandbox Code Playgroud)

我在Interface builder中需要做什么?我添加了Web视图,是否需要以任何方式链接它?

事情是我添加了简单的文本字段(并将其链接到Web视图)并且当我在那里输入它时它加载URL就好了,但它没有这个代码,任何想法?

Pat*_*ini 6

在您的XIB文件中,您应该有一个App Delegate对象(以及其他一些东西).

对象

右键单击它会显示一个HUD列表,您可以在其中看到每个插座(以及其他一些东西).找到webber插座,然后单击右侧的圆圈.然后拖到你的WebView.

链接

您刚刚完成的工作是将属性链接到XIB对象.跳过此步骤,您的App Delegate将不知道所指的webber是什么.

另请注意,为了填充该HUD列表,您的属性需要专门标记IBOutlet.