我尝试在OS X 10.8中使用Objective-C编写单窗口应用程序来测试一些东西。除了我创建的NSTextField没有响应键盘输入外,一切正常。代码如下:
#import <Cocoa/Cocoa.h>
@interface customizedView:NSView
+(customizedView *)aCustomizedView; // changed method name to follow convention
@end
@implementation customizedView
+(customizedView *)aCustomizedView{
customizedView *newView = [[[customizedView alloc] initWithFrame:NSMakeRect(0,0,400,300)] autorelease];
NSTextField *tf = [[[NSTextField alloc] initWithFrame:NSMakeRect(200,150,150,50)] autorelease];
[newView addSubview: tf positioned:NSWindowBelow relativeTo:newView];
return newView;
}
-(void)windowWillClose:(NSNotification *)aNot{
[NSApp terminate:self];
}
@end
void setup(){
NSWindow *aWindow = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,400,300) styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:2 defer:NO] autorelease];
customizedView *aView = [customizedView aCustomizedView];
[aWindow setContentView:aView];
[aWindow setDelegate:aView];
[aView setNeedsDisplay:YES];
[aWindow makeKeyAndOrderFront:nil];
}
int main(int argc, char **argv){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSApplication *NSApp = [NSApplication sharedApplication];
setup();
[NSApp run]; [NSApp release]; [pool release];
exit(EXIT_SUCCESS);
}
使用
进行编译gcc -w filename.m -o tmp -framework Cocoa
除了我无法使用键盘输入我定制的NSView中的文本字段外,一切正常。使用鼠标粘贴是可以的。到目前为止,我仍然没有理解为什么会发生这种情况,我提前感谢所有真诚地阅读本文的人。