Tam sú niektoré časti dokumentácie ste prepojenie, ktoré je relevantné, že si myslím, že môže byť zanedbané.
Som kopíroval pár riadkov nižšie:
Ak sa rozhodnete držať pomocou NSTextField, čo tabulátora a/alebo umožňuje zadať a vrátiť kľúče pre line-prestávky možno dosiahnuť realizáciou nasledovných delegovať metóda:
- (BOOL)ovládanie:(NSControl*)ovládanie textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector;
Poznámka: Pri plnení tohto delegáta metódy na svoje vlastné objektu , mali by ste nastaviť váš objekt sa ako "delegovať" pre tento NSTextField.
Som tučne niekoľko callouts ktoré si myslím, že by mohol ujsť.
Táto metóda je v rámci NSControlTextEditingDelegate
protokol v rámci NSControl.h. Ako by sa mali realizovať triedy, ktorá implementuje NSControlTextEditingDelegate
(napr. NSTextFieldDelegate
)
Jeden spoločný spôsob, ako to urobiť, je mať ViewController "držať" na NSTextField byť NSTextFieldDelegate
.
Tu je veľmi jednoduchý príklad používaní vzorového kódu z Apple si prepojené:
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
Potom nastavte NSTextField je delegovať na ViewController
Nie je potrebné pridávať vlastné podtrieda.
Alternatívne pravdepodobne by ste mohli urobiť vlastný text oblasti podtrieda vlastného delegáta. Niečo spolu tieto riadky:
#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