当点击外部时,如何放弃对NSTextField的关注

时间:2022-08-26 20:02:12

I've a NSView with a editable NSTextField and multiple other subviews like NSView, NSSlider, NSImage etc.

我有一个NSView有一个可编辑的NSTextField和其他多个子视图,比如NSView, NSSlider, NSImage等等。

  1. After I've entered my text into the editable NSTextField and click on any of the other views, move the slider etc. I would like my NSTextField to lose focus. I've tried calling the resignFirstResponder, but that does not seems to work. How can I do this?
  2. 在我将文本输入到可编辑的NSTextField并单击任何其他视图之后,移动滑块等等。我希望我的NSTextField失去焦点。我试过调用resignFirstResponder,但这似乎不起作用。我该怎么做呢?
  3. When I mark the text inside my NSTextField a blue background behind the text is shown. How can I change the color is this?
  4. 当我在NSTextField内标记文本时,会显示文本后面的蓝色背景。我怎么能改变颜色呢?

3 个解决方案

#1


9  

Suppose that you have a subclass of NSView called clickView1.h. In reference to this post, you can achieve your goal in No. 1 as follows.

假设有一个叫clickView1.h的NSView子类。关于这篇文章,你可以在下面的文章中实现你的目标。

- (void)mouseDown:(NSEvent *)event{
    AppDelegate *appDelegate = (AppDelegate *)[NSApp delegate];
    [appDelegate.window makeFirstResponder:nil];
}

As for No. 2, I don't understand the question.

至于二号,我不明白这个问题。

#2


3  

For question 1, I agree with BlueTomato that you need to make something else first responder, not call resignFirstResponder. For question 2, subclass NSTextFieldCell, and in the subclass, have an override like this:

对于问题1,我同意BlueTomato的观点,你需要做一些其他的事情,而不是调用resignFirstResponder。对于问题2,NSTextFieldCell的子类以及子类中有这样的重写:

- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
    [super setUpFieldEditorAttributes: textObj];

    if ([textObj isKindOfClass: [NSTextView class]])
    {
        NSTextView* textView = (NSTextView*) textObj;

        [textView setSelectedTextAttributes:
            [NSDictionary dictionaryWithObjectsAndKeys:
                [NSColor redColor],
                NSBackgroundColorAttributeName,
                nil] ];
    }

    return textObj;
}

#3


0  

Try the following method:

试试下面的方法:

 [[NSApp mainWindow] performSelector:@selector(resignFirstResponder:)
                          withObject:yourTextfield
                          afterDelay:0.0];

#1


9  

Suppose that you have a subclass of NSView called clickView1.h. In reference to this post, you can achieve your goal in No. 1 as follows.

假设有一个叫clickView1.h的NSView子类。关于这篇文章,你可以在下面的文章中实现你的目标。

- (void)mouseDown:(NSEvent *)event{
    AppDelegate *appDelegate = (AppDelegate *)[NSApp delegate];
    [appDelegate.window makeFirstResponder:nil];
}

As for No. 2, I don't understand the question.

至于二号,我不明白这个问题。

#2


3  

For question 1, I agree with BlueTomato that you need to make something else first responder, not call resignFirstResponder. For question 2, subclass NSTextFieldCell, and in the subclass, have an override like this:

对于问题1,我同意BlueTomato的观点,你需要做一些其他的事情,而不是调用resignFirstResponder。对于问题2,NSTextFieldCell的子类以及子类中有这样的重写:

- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
    [super setUpFieldEditorAttributes: textObj];

    if ([textObj isKindOfClass: [NSTextView class]])
    {
        NSTextView* textView = (NSTextView*) textObj;

        [textView setSelectedTextAttributes:
            [NSDictionary dictionaryWithObjectsAndKeys:
                [NSColor redColor],
                NSBackgroundColorAttributeName,
                nil] ];
    }

    return textObj;
}

#3


0  

Try the following method:

试试下面的方法:

 [[NSApp mainWindow] performSelector:@selector(resignFirstResponder:)
                          withObject:yourTextfield
                          afterDelay:0.0];