3

我尝试在 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 中的文本字段之外,一切正常。用鼠标粘贴是可以的。到目前为止,我仍然不知道为什么会发生这种情况,我提前感谢所有真诚阅读此提示的人。

4

0 回答 0