Re: shouldChangeTextInRanges and undo
Re: shouldChangeTextInRanges and undo
- Subject: Re: shouldChangeTextInRanges and undo
- From: Ken Victor <email@hidden>
- Date: Thu, 10 Nov 2005 18:05:36 -0800
thanx,
that solves the problem.
ken
At 5:56 PM -0800 11/10/05, Douglas Davidson wrote:
On Nov 10, 2005, at 5:39 PM, Ken Victor wrote:
to see this, i created an almost codeless cocoa doc based app. in
the doc window nib, i replaced the NSTextField with an undoable
NSTextView and gave it a custom class. the custom class consists of
only one method as follows:
- (BOOL) shouldChangeTextInRanges: (NSArray*) affectedRanges
replacementStrings: (NSArray*) replacementStrings {
static NSMutableCharacterSet* cSet = nil;
BOOL result = [super shouldChangeTextInRanges: affectedRanges
replacementStrings: replacementStrings];
It's easier to do this in a delegate method, but if you are going to
override shouldChange... then you have to do thing s in a different
order. You should do your check first, and if you are going to
prohibit the change, just return NO without calling the super
method. If you are going to allow the change, return the return
value of the super method. I've roughly rearranged your
implementation below.
Douglas Davidson
- (BOOL) shouldChangeTextInRanges: (NSArray*) affectedRanges
replacementStrings: (NSArray*) replacementStrings {
static NSMutableCharacterSet* cSet = nil;
if (!cSet) {
cSet = [NSMutableCharacterSet controlCharacterSet];
[cSet removeCharactersInString: @"\t"];
[cSet retain];
}
NSEnumerator* en = [replacementStrings objectEnumerator];
while (NSString* aRepStr = [en nextObject]) {
NSRange foundRange = [aRepStr rangeOfCharacterFromSet: cSet];
if (foundRange.location != NSNotFound) {
NSBeep();
return NO;
}
}
return [super shouldChangeTextInRanges: affectedRanges
replacementStrings: replacementStrings];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden