2

我一直在阅读Cocoa 事件处理指南以了解如何为我的应用程序处理键盘事件。但是,我不太确定我找到的解决方案是否是解决这个问题的惯用方法,甚至是最好的方法。

我有一个名为的视图和一个名为的CustomView控制器CustomViewController,其中包含一个NSTextView(在 a 内NSScrollView)和一个NSTextField. 基本上是一个聊天窗口。

我想要的是,即使NSTextField没有选择 ,并且用户开始输入,NSTextField也应该将其作为第一响应者,并且用户输入的文本应该附加到NSTextField.

我提出了一个解决方案(如下),在该解决方案中我RootWindowController捕获keyDown事件、成为NSTextField第一响应者、获取字段编辑器并附加文本。

这是 Cocoa 开发人员采用的惯用方法,还是我缺少的一种更简单的方法?代码如下。谢谢。

根窗口控制器.m

#import "RootWindowController.h"

@implementation RootWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        self.customViewController = [[CustomViewController alloc] init];

        NSView *contentView = self.window.contentView;
        CGFloat viewWidth = contentView.frame.size.width;
        CGFloat viewHeight = contentView.frame.size.height;

        self.customViewController.customView.textField.frame =
            NSMakeRect(0, 0, viewWidth, 50);
        self.customViewController.customView.scrollView.frame = 
            NSMakeRect(0, 51, viewWidth, viewHeight - 50);
        self.customViewController.customView.textView.frame = 
            NSMakeRect(0, 51, viewWidth, viewHeight - 50);

       self.window.contentView = self.customViewController.customView;
    }

    return self;
}

- (void)keyDown:(NSEvent *)theEvent
{    
    [self.window 
       makeFirstResponder:self.customViewController.customView.textField];

    NSString *characters = theEvent.characters;    
    NSText *fieldEditor = [self.window fieldEditor:YES
       forObject:self.customViewController.customView.textField];
    [fieldEditor setString:
       [NSString stringWithFormat:@"%@%@", fieldEditor.string, characters]];
    [fieldEditor setSelectedRange:NSMakeRange(fieldEditor.string.length, 0)];
    [fieldEditor setNeedsDisplay:YES];
}

@end

自定义视图控制器.m

#import "CustomViewController.h"

@implementation CustomViewController

- (id)init
{
    self = [super init];
    if (self) {
        self.customView = [[CustomView alloc] initWithFrame:NSZeroRect];
        [self.customView setAutoresizesSubviews:YES];

        self.view = self.customView;
    }
    return self;
}

@end

自定义视图.m

#import "CustomView.h"

@implementation CustomView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
        self.textView   = [[NSTextView alloc] initWithFrame:NSZeroRect];
        self.textField  = [[NSTextField alloc] initWithFrame:NSZeroRect];

        [self.textView setAutoresizesSubviews:YES];

        [self.scrollView setDocumentView:self.textView];
        [self.scrollView setHasVerticalScroller:YES];
        [self.scrollView setAutoresizesSubviews:YES];

        [self.textField setStringValue:@"Placeholder Text"];

        [self addSubview:self.scrollView];
        [self addSubview:self.textField];

        [self.textView setNeedsDisplay:YES];
    }
    return self;
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

@end
4

0 回答 0