Il y a quelques parties de la documentation liée qui est pertinente qui, je pense, ont été négligées.
J'ai copié quelques lignes ci-dessous:
Si vous décidez de continuer à utiliser NSTextField, permettant la touche de tabulation et/ou permettant d'entrer et de rendement clés pour les sauts de lignes peut être atteint par la mise en œuvre de la suite de délégué de la méthode:
- (BOOL)de contrôle:(NSControl*)contrôle textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector;
Remarque: Lors de la mise en œuvre de ce délégué méthode dans votre propre objet , vous devez définir votre objet comme le "délégué" pour ce NSTextField.
J'ai mis en caractères gras un peu de la légende qui, je pense, ont été manqués.
Cette méthode est à l'intérieur de la NSControlTextEditingDelegate
protocole au sein de NSControl.h. Comme telle, elle doit être implémentée par une classe qui implémente l' NSControlTextEditingDelegate
(c'est à dire NSTextFieldDelegate
)
Une façon courante de le faire est d'avoir le ViewController "holding" le NSTextField être le NSTextFieldDelegate
.
Voici un exemple très simple à l'aide de l'exemple de code à partir d'Apple est lié:
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
Ensuite, réglez votre NSTextField déléguée pour la ViewController
Pas besoin d'ajouter une sous-classe personnalisée.
Sinon, vous pourriez probablement faire le champ texte personnalisé sous-classe de ses propres délégués. Quelque chose le long de ces lignes:
#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