Há algumas partes da documentação ligado que é pertinente que eu acho que pode ter sido negligenciado.
Que eu tenha copiado algumas das linhas abaixo:
Se você decidir continuar usando o NSTextField, permitindo a tecla tab e/ou permitindo entrar e voltar chaves para quebras de linha pode ser alcançado através da implementação das seguintes delegado método:
- (BOOL)controle:(NSControl*)controle textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector;
Nota: Ao implementar esse delegado método em seu próprio objeto , você deve definir o seu objeto como "delegado" para este NSTextField.
Eu negrito alguns dos textos explicativos que eu acho que pode ter sido perdido.
Este método é dentro do NSControlTextEditingDelegate
protocolo dentro de comando NSControl.h. Como tal, deve ser implementada por uma classe que implementa a NSControlTextEditingDelegate
(i.é. NSTextFieldDelegate
)
Uma maneira comum de fazer isso é ter o ViewController "segurando" o NSTextField ser o NSTextFieldDelegate
.
Aqui está um exemplo muito simples utilizar o código de exemplo da Apple ligado:
ViewController.h
#import <Cocoa/Cocoa.h>
@interface ViewController : NSViewController <NSTextFieldDelegate>
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
BOOL result = NO;
if (commandSelector == @selector(insertNewline:))
{
// new line action:
// always insert a line-break character and don’t cause the receiver to end editing
[textView insertNewlineIgnoringFieldEditor:self];
result = YES;
}
else if (commandSelector == @selector(insertTab:))
{
// tab action:
// always insert a tab character and don’t cause the receiver to end editing
[textView insertTabIgnoringFieldEditor:self];
result = YES;
}
return result;
}
@end
Em seguida, defina o NSTextField delegado para o ViewController
Não há necessidade de adicionar uma personalizada subclasse.
Alternativamente, você poderia provavelmente fazer com que o campo de texto personalizado subclasse seu próprio representante. Algo ao longo dessas linhas:
#import "MyTextFieldSubclass.h"
@interface MyTextFieldSubclass() <NSTextFieldDelegate>
@end
@implementation MyTextFieldSubclass
- (instancetype)init {
self = [super init];
if (self) {
self.delegate = self;
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.delegate = self;
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.delegate = self;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
BOOL result = NO;
if (commandSelector == @selector(insertNewline:))
{
// new line action:
// always insert a line-break character and don’t cause the receiver to end editing
[textView insertNewlineIgnoringFieldEditor:self];
result = YES;
}
else if (commandSelector == @selector(insertTab:))
{
// tab action:
// always insert a tab character and don’t cause the receiver to end editing
[textView insertTabIgnoringFieldEditor:self];
result = YES;
}
return result;
}
@end