Re: Modify Input in NSTableView
Re: Modify Input in NSTableView
- Subject: Re: Modify Input in NSTableView
- From: "K. Darcy Otto" <email@hidden>
- Date: Mon, 31 Mar 2008 17:18:23 -0700
- Resent-date: Mon, 31 Mar 2008 18:44:26 -0700
- Resent-from: "K. Darcy Otto" <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: Cocoa Developer List <email@hidden>
Closing in on a solution (but not quite there – see below for
details):
I have made some progress in solving this problem, but still have one
strangeness (detailed below). Here is what I have done to get the ">"
replaced with "→" in an NSTableView:
1. Set my AppController as the window delegate.
2. Implemented windowWillReturnFieldEditor: as follows:
-(id)windowWillReturnFieldEditor:(NSWindow *)window toObject:
(id)anObject
{
if ([anObject isKindOfClass:[DeductionTable class]])
{
return fieldEditor;
}
return nil;
}
(The table in my window is a DeductionTable object, subclass
NSTableView. fieldEditor is an object of class
DeductionTableFieldEditor, a subclass of NSTextField.)
3. I then implement controlTextDidChange as follows:
-(void)controlTextDidChange:(NSNotification *)aNotification
{
// Trap letters in DeductionTableFieldEditor
NSMutableString *fieldString = [NSMutableString stringWithString:
[fieldEditor string]];
NSUInteger changeCount = [fieldString replaceOccurrencesOfString:@">"
withString:[NSString stringWithFormat:@"%C",0x2192]
options:NSLiteralSearch
range:NSMakeRange(0,[fieldString length])];
if (changeCount > 0 && [fieldEditor
shouldChangeTextInRange:NSMakeRange(0,[fieldString length])
replacementString:fieldString])
{
[fieldEditor setString:fieldString];
[fieldEditor didChangeText];
}
}
So, this creates the result I want: when the user types ">", it is
automatically replaced by "→". The problem I'm having is that
hitting return or tab no longer ends editing of the current cell. I
have tried implementing doCommandBySelector: to take care of this, but
as soon as I do, I lose return, tab and the arrow keys. Any ideas?
On 31-Mar-08, at 11:26 AM, K. Darcy Otto wrote:
On Mar 31, 2008, at 00:29, Quincey Morris wrote:
> On Mar 30, 2008, at 23:08, K. Darcy Otto wrote:
>
>> I'm working on an application in which users enter data into a
>> table, but I need to substitute a greater-than symbol (>) for a
>> right-arrow symbol (→, unicode: 2192) as the user presses they
key
>> (or copies the text, or whatever). After looking at the docs and
>> this list, I'm not much closer to determining the best way to do
>> this (or even how to do it). So far, I have been thinking I have
>> to do something with NSWindow and the fieldEditor; perhaps setting
>> the window's field editor to be a particular NSTextView, and
>> implement some method in that text view (insertText:?) to make
the
>> substitution. I take it that working with keyDown: would not be
>> the best approach. Would this be on the right track? Any help
>> would be appreciated.
>
> You could try adding a validate<Key> method for the property that
> holds the string value. In that method, take the input string and
> create a new string, replacing whatever characters you need to.
Pass
> the new string back in the first parameter, and return YES from the
> method.
>
> If I haven't overlooked something obvious, this would work no
matter
> whether the text was typed or pasted into the editing field.
>
> Oh -- make sure you check "Validates immediately" for the table
> column binding in IB, too.
Of course, this is only going to make the replacement happen when the
user presses return or tab. To get it to happen as soon as a
replaceable character is typed, you'd have to convince the field
editor to validate after every keystroke or editing action. So maybe
this is not the best way.
This is exactly the problem I'm running into. I can get changes to
be made after the user exits the editing field, but I cannot get the
changes to be made on-the-fly. After some additional reading, I
have tried to solve this problem by implementing the following in
the NSWindow delegate (where DeductionTable is my NSTableView
subclass, and DeductionTableFieldEditor is my NSTextView subclass):
-(id)windowWillReturnFieldEditor:(NSWindow *)window toObject:
(id)anObject
{
if ([anObject isKindOfClass:[DeductionTable class]])
{
return [[DeductionTableFieldEditor alloc] init];
}
return nil;
}
Then, I started playing around with -(BOOL)textView:(NSTextView
*)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange
replacementString:(NSString *)replacementString and -
(BOOL)textShouldBeginEditing:(NSText *)aTextObject; but all I've
succeeded in doing is messing up the editing functions that were
there previously. Any other suggestions?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden