• 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: Out-of-order Undos and NSTextField [SOLVED, well sort of]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]


  • Subject: Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]
  • From: James Bucanek <email@hidden>
  • Date: Tue, 11 Jul 2006 12:05:53 -0700

After messing around with this problem, I found an almost acceptable solution. This is a bit hacky, and I fear there are unpredicatble side-effect, but it seems to be working in my application OK.

The hack is to sub-class NSTextField, then intercept one of the delegate messages sent to NSTextField from the NSTextView that gets created as the field's editor. I wish there was a textViewWillBeginEditing:, but I settled for textView:shouldChangeTextInRange:replacementString: which gets invoked whenever the user tries to add/remove/replace any text. Before the message is passed on to the real NSTextField, I simply turn off undo support in the field editor.

Hopefully this will solve someone else's problem, or at least encourage someone to find a better solution.


------------- /Users/james/Desktop/UndoableTextField.h

//
//  UndoableTextField.h
//  QRecall
//
//  Created by James Bucanek on 7/11/06.
//

#import <Cocoa/Cocoa.h>

//
//  An NSTextField that does not use the undo manager.
//
// NSTextField/NSTextView has this nasty habid of registering undo actions out of order.
// This is used in windows where the text field is bound to a property that is already
//  registering undo actions based on the changes in its value and we don't need the field
//  editor registering superfluous text edit changes at the same time.
//
// Notes:
// The object works by intercepting the textView:shouldChangeTextInRange:replacementString:
//  delegate message sent to the text field whenever the user tries to change the text.
//  Before passing the message on, it disables undo on the NSTextView that is being used
//  as the field editor.
// This won't work for more complex cases where the user changes the font or
//  other attributes of the text first. It also might mess up other fields that reuse
//  the NSTextView object.


@interface UndoableTextField : NSTextField

- (BOOL)textView:(NSTextView*)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString*)replacementString;

@end


------------- /Users/james/Desktop/UndoableTextField.m

//
//  UndoableTextField.m
//  QRecall
//
//  Created by James Bucanek on 7/11/06.
//

#import "UndoableTextField.h"


@implementation UndoableTextField

- (BOOL)textView:(NSTextView*)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString*)replacementString
{
    [textView setAllowsUndo:NO];    // turn off Undo before the user changes anything
    // NSTextField should implement textView:shouldChangeTextInRange:replacementString:, but just to be on the safe side...
    if ([super respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementString:)])
        return ([super textView:textView shouldChangeTextInRange:affectedCharRange replacementString:replacementString]);
    return (YES);
}

@end
--
James Bucanek
 _______________________________________________
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

  • Follow-Ups:
    • Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]
      • From: Ken Victor <email@hidden>
References: 
 >Re: Out-of-order Undos and NSTextField (From: James Bucanek <email@hidden>)

  • Prev by Date: Printing - Why are my PDF files so big?
  • Next by Date: Re: Best way to discover resource forks?
  • Previous by thread: Re: Out-of-order Undos and NSTextField
  • Next by thread: Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]
  • Index(es):
    • Date
    • Thread