【发布时间】:2012-08-30 10:31:14
【问题描述】:
我正在学习 ObjC 和可可开发,遇到了一个真正的“笨蛋”。筋疲力尽的谷歌,我恭恭敬敬地戴上我的绝望帽子并向你展示:
一个类和一个视图控制器:
“内容窗口”类导入一个视图控制器实例并将其放置在一个窗口中:
内容窗口.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"];
这成功地将视图元素“标题”设置为“我的标题”。
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
然而,这会引发错误:
Receiver type 'WebFrame' for instance message is a forward declaration.
并用红色下划线的部分:
viewController.webView mainFrame
我的视图控制器如下:
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 基础,这是在另一个线程中建议的。
我一辈子都无法理解发生了什么。但在我这样做之前,碰巧:
有人可以帮忙吗?
一如既往地感谢 AtFPt。
【问题讨论】:
标签: objective-c xcode cocoa interface-builder forward-declaration