0

我有点期望这是 NSTextField 的默认行为,以自动响应带有插入符号移动的左/右箭头键。不幸的是,在我的应用程序中这不会发生。是否可以选择打开该行为,还是我必须自己添加?

为了进一步解释,接口是动态生成的。我有视图(包含 NSTextFields 的 xib)及其视图控制器,我(手动)堆叠在另一个视图上。创建视图(即其控制器(newView))时,我使用此代码将其添加到我的 NSBox 的 contentView(boxContent)中:

[boxContent addSubview:[newView view]];
[newView setWithLabel:attributeLabel forProperty:attribute];
newViewY -= [[newView view] frame].size.height;
[[newView view] setFrameOrigin:NSMakePoint(0, newViewY)];
newViewY -=padding;

奇怪的是文本输入和退格键起作用,而其他键(箭头、删除)不起作用。

这可能是 NSResponder 链的问题吗?那么为什么文本输入起作用呢?

4

1 回答 1

0

So, I do not know what is the cause of these problems, but this is the roundabout solution I made.

I created a subclass of NSTextField that sends commands (selectors) to its NSTextView. NSTextField has one subview and that subview has yet another subview that is actually the NSTextView:

NSTextView* textView = nil;    
NSArray* subs = [self subviews];
if( subs && [subs count] > 0 ){
    NSView* firstView = [subs objectAtIndex:0];
    NSArray* firstSubs = [firstView subviews];
    if (firstSubs && [firstSubs count] > 0) {       
        textView = [firstSubs objectAtIndex:0];
    }
}

When I initialise that one (if anyone knows a better way than this subview search, please tell), I send it the commands that it was supposed to get:

-(void)keyUp:(NSEvent *)theEvent{   
   BOOL isShift = (theEvent.modifierFlags & NSShiftKeyMask) > 0;
   BOOL isCommand = (theEvent.modifierFlags & NSCommandKeyMask ) > 0;
   switch (theEvent.keyCode) {
      case 7: if( isCommand && !isShift ) [self notifyTextView:@selector(cut:)]; break;
      case 8: if( isCommand && !isShift ) [self notifyTextView:@selector(copy:)]; break;
      case 9: if( isCommand && !isShift ) [self notifyTextView:@selector(paste:)]; break;
      case 123: [self notifyTextView:isShift ? @selector(moveLeftAndModifySelection:) : @selector(moveLeft:)];break;
      case 124: [self notifyTextView:isShift ? @selector(moveRightAndModifySelection:) :@selector(moveRight:)];break;
      case 117: [self notifyTextView:@selector(deleteForward:)];break;
      default: break;
   }
   [super keyUp:theEvent];
}

Might not be the most elegant solution, but it works (until I figure out what went wrong in the first place).

于 2013-07-19T07:12:52.267 回答