• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Controlling selection in NSTextFields
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Controlling selection in NSTextFields


  • Subject: Re: Controlling selection in NSTextFields
  • From: Rainer Brockerhoff <email@hidden>
  • Date: Sun, 11 Nov 2001 12:07:08 -0200

Okay, it's solved. Here's a write-up for whoever's interested. Thanks to Erik M. Buck and several others who wrote in with suggestions!

I've got NSTextFields where I want to keep the user from selecting, or typing over, certain characters. (Look at the Type/Creator/Extension panel in the next version of XRay, which should be coming out late this coming week, to see exactly where and why.)

This is straight out of my actual source code (some names changed to protect the irrelevant). In my document class, which is the window delegate:

- (id)windowWillReturnFieldEditor:(NSWindow*)sender toObject:(id)anObject {
if ([anObject respondsToSelector:@selector(delegate)]) {
id del = [anObject delegate];
if ([del respondsToSelector:@selector(getMyFieldEditor)]) {
return [del getMyFieldEditor];
}
}
return nil;
}

Here's the declaration of my custom field editor:

@interface MyFieldEditor : NSTextView {
}
+ (MyFieldEditor*)mySharedEditor;
@end

@implementation MyFieldEditor
+ (MyFieldEditor*)mySharedEditor {
static MyFieldEditor* editor = nil;
if (!editor) {
editor = [[MyFieldEditor alloc] initWithFrame:NSZeroRect];
[editor setFieldEditor:YES];
}
return editor;
}
- (void)setSelectedRange:(NSRange)charRange affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag {
// ...
// check charRange and correct it if necessary
// this appears to be the only method called both for mouse and keyboard events...
// ...
[super setSelectedRange:charRange affinity:affinity stillSelecting:stillSelectingFlag];
}
@end

Finally, in my model class, which is the NSTextField's delegate, I implemented :

- (id)getMyFieldEditor {
return [MyFieldEditor mySharedEditor];
}

And as a bonus, my formatter was simplified, since it now just has to concern itself with contents and not with selections.

This delegation stuff is really powerful, especially when combined with respondsToSelector:. Simple and elegant, once one knows where to hook into things... thanks again to everybody.

--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"Originality is the art of concealing your sources."
http://www.brockerhoff.net/ (updated Oct. 2001)


References: 
 >Re: Controlling selection in NSTextFields (From: "John C. Randolph" <email@hidden>)

  • Prev by Date: Re: AltiVec on OSX in Cocoa
  • Next by Date: Re: New Cocoa Programmer
  • Previous by thread: Re: Controlling selection in NSTextFields
  • Next by thread: Can't cache image...
  • Index(es):
    • Date
    • Thread