How do I cut and paste text from within a NSTextFieldCell in an NSOutlineView

I've programmed iOS since the beginning, but am new to Cocoa so please be gentle!

I have an NSOutlineView and have implemented cut/copy/paste from the main menu to cut/copy/paste the selected rows.

Now I also want to allow the user to select some text within an NSTextFieldCell, copy it, place the cursor in a different NSTextFieldCell, and paste it.

I have managed to discover that the user is working inside a cell, by setting a BOOL in:

- (BOOL)outlineView:(NSOutlineView *)anOutlineView shouldEditTableColumn:(NSTableColumn*)aTableColumn item:(id)anItem

and unsetting it in my notification for the end of editing, set up like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidEnd:)
                                                 name:NSControlTextDidEndEditingNotification object:nil];

This seems to work fine.

Then, in my cut, copy, paste actions, I can check the BOOL and do the right thing - copy whole rows if the user is not working within a cell, and copy text if the user is working within a cell.

However, I just can't find out how to get at what I need when the user is working within the cell.

I've considered using the NSText cut:, copy: paste: methods, as they should handle selection for me. But I don't think I have an NSText object anywhere!

Alternatively, I need to be able to read off the selected text from my NSTextFieldCell, save it, then discover the insertion point in the cell to be pasted into, and paste.

Or is there much better built-in support that I'm missing?

Any help gratefully received - particular solutions of course, but also links to background primers on text / cell / field handling in Cocoa.


As is always the way, I find a solution just after posting!

The answer is that the field editor, an NSTextView, is the first responder when the cell is being edited. Its superclass, NSText, is the one that supports cut:, copy: and paste:.

So my "cell edit" versions of the cut copy paste commands look like this:

-(void)pasteTextToCell:(id)sender {

    NSTextView* fieldEditor = (NSTextView*)[[appDelegate mainWindow] firstResponder];
    [fieldEditor paste:sender];
}

-(void)copyTextFromCell:(id)sender {

    NSTextView* fieldEditor = (NSTextView*)[[appDelegate mainWindow] firstResponder];
    [fieldEditor copy:sender];
}

-(void)deleteTextFromCell:(id)sender {

    NSTextView* fieldEditor = (NSTextView*)[[appDelegate mainWindow] firstResponder];
    [fieldEditor cut:sender];
}
链接地址: http://www.djcxy.com/p/85210.html

上一篇: 在NSTextFieldCell中拦截Keydown操作

下一篇: 如何从NSOutlineView中的NSTextFieldCell中剪切和粘贴文本