【问题标题】:WebFrame content not appearingWebFrame 内容未出现
【发布时间】:2015-09-17 23:55:49
【问题描述】:

我正在使用一个简单的小应用程序来尝试学习在 OS X 桌面应用程序中使用 WebFrames。

WebFrame 存在于通过单击主菜单 nib 窗口中的按钮打开的窗口中。

我试图在加载窗口时将一个简单的 HTML 字符串加载到 WebView 中,以确保一切都正确连接。

这是窗口的 h 文件

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

@interface WebReportView : NSWindowController

@property IBOutlet WebView *webView;

@end

这是 m 文件,包括我认为应该加载 HTML 字符串的代码。

#import "WebReportView.h"

@implementation WebReportView

- (void)windowDidLoad {
    [super windowDidLoad];
    [[self.webView mainFrame]loadHTMLString:@"<html><head></head><body><h1>Foo</h1></body></html>" baseURL:nil];
}

@end

窗口加载得很好,我可以看到 WebView,但内容没有加载到 WebView 中。大粗体“Foo”不见了。

如果我尝试加载实际的网页内容,也会发生同样的事情:

NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[self.webView.mainFrame loadRequest:request];

没有任何东西被加载到 WebView 中。

我知道 loadHTMLString(或 loadRequest)方法会被调用,因为我在它之前和之后放置了 NSLogs。

我错过了什么?

【问题讨论】:

  • 嘿cutmancometh,我面临同样的问题。你有什么解决办法吗?请让我知道...这对我来说非常紧急。提前致谢。

标签: objective-c xcode macos cocoa


【解决方案1】:

确保您已创建 WebView Outlets 并建立了所有连接。 确保您已设置 WebView Delegate

 NSString *fullURL  = @"http://www.google.com";
 NSURL *url = [NSURL URLWithString:fullURL];

 NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
 [webUrl loadRequest:requestObj];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 2015-12-07
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多