Hay un par de partes de la documentación vinculada que es pertinente que creo que puede haber sido descuidado.
He copiado algunas de las líneas más abajo:
Si usted decide seguir utilizando NSTextField, permitiendo la tecla tab y/o permitiendo entrar y volver claves para los saltos de línea se puede lograr mediante la aplicación de los siguientes delegado método:
- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector;
Nota: Cuando la aplicación de este delegado método en su propio objeto debe establecer su objeto como el "delegado" para este NSTextField.
He resaltado algunas de las llamadas que creo que podría haber sido pasada por alto.
Este método está dentro de la NSControlTextEditingDelegate
protocolo dentro de NSControl.h. Como tal debe ser implementado por una clase que implementa la NSControlTextEditingDelegate
(es decir, NSTextFieldDelegate
)
Una manera común de hacer esto es tener el ViewController "explotación" de la NSTextField ser el NSTextFieldDelegate
.
He aquí un ejemplo muy sencillo utilizar el código de ejemplo de Apple, vinculado:
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
A continuación, establezca su NSTextField del delegado para el ViewController
No hay necesidad de agregar una subclase personalizada.
Alternativamente, usted probablemente podría hacer que el campo de texto personalizado subclase su propio delegado. Algo a lo largo de estas líneas:
#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