我正在学习 ObjC 和 cocoa dev,并且遇到了一个真正的“难题”。在用尽谷歌之后,我恭敬地戴上我的绝望帽子并向您展示:
一个类和一个 View Controller :
“内容窗口”类导入 View Controller 实例并将其放置在窗口中:
ContentWindow.h
#import <Foundation/Foundation.h>
#import <WebKit/WebView.h>
#import "ContentViewController.h"
@interface ContentWindow : NSWindow{
ContentViewController* viewController;
}
@property IBOutlet ContentViewController* viewController;
-(NSWindow *) newWindow;
@end
内容窗口.m
#import "ContentWindow.h"
@implementation ContentWindow
@synthesize viewController;
-(NSWindow *) newWindow{
//Builds the window as 'window' and displays it successfully here
//... [code redacted for brevity]
// Build view
viewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
[window setContentView: viewController.view];
NSString *urlString = @"http://www.google.com";
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
[viewController.title setStringValue:@"my title"];
}
@end
我尝试使用界面做两件事:
[viewController.title setStringValue:@"my title"];
这成功地将 View 元素“标题”设置为“我的标题”。
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
但是,这会引发错误:
Receiver type 'WebFrame' for instance message is a forward declaration.
并用红色下划线表示该行的部分:
viewController.webView mainFrame
我的 View Controller 如下:
ContentViewController.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
@interface ContentViewController : NSViewController {
IBOutlet NSTextField *title;
IBOutlet WebView *webView;
}
@property IBOutlet WebView *webView;
@property IBOutlet NSTextField *title;
@end
ContentViewController.m
#import "ContentViewController.h"
@interface ContentViewController ()
@end
@implementation ContentViewController
@synthesize title, webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
@end
最后为了使用这个类,我从我的 AppDelegate 类实例化一个内容窗口
contentWindow = [[ContentWindow new] newWindow];
将 ContentWindow.h 导入 AppDelegate.h 并设置:
__strong NSWindow * contentWindow
作为 AppDelegate 合成实例变量。
我已经在 IB 中链接了这两个项目(当然!)我还在我的项目中添加了 Webkit 基础,这是在另一个线程中建议的。
我一生都无法理解发生了什么。我知道合理的答案是放下 Xcode,拿起《学习 Xcode 和 Objective c》这本书(读到一半时有一个书签,我傲慢地认为我已经学到了足够多的知识来尝试一些东西),但是在我这样做之前,偶尔:
有人可以帮忙吗?
一如既往地感谢 AtFPt。
最佳答案
通常此错误消息意味着不知道类的类型(因为由 @class 声明)。
确保您的代码可以看到 WebFrame
的声明。
如果是这样,也许您稍后添加它,并且 XCode 可以使用较旧的元数据。在这种情况下,构建前进行清理通常会有所帮助。
关于objective-c - 接收者类型 'WebFrame' 例如消息是前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12194865/