NSTextField gets focus but no text input cursor is visible

NSTextField is not working as expected in my case. What I got is:

  • an NSTextField control on a view, that is shown in a window, that is not a main app window.

  • this text field is not visible initially - it is placed on a view that animates its height when "Edit Subject" menu item is selected (custom popup menu)

  • this animated view has also other controls - two buttons: one deletes text in text field, other is "Done"

  • the window also has a custom NSText control which mostly has the focus on (chat's text input control)

  • the chat window (the window I'm working on) has also a list of conversations.

  • I'm doing the steps:

  • when I right click on conversation (from the list), popup menu shows up.
  • I select Edit subject menu item
  • view with NSTextField expands a little bit, NSTextField should activate, and I expect, there will be a blinking cursor activated in this NSTextField
  • BUT, there is no cursor blinking. I can edit, and cursor shows when I start typing, so I presume, the text field has focus. I need to have cursor blinking there immediately after the view expands.

    To animate this "Edit Subject" view's height I use NSAnimationContext runAnimationGroup and in completion handler I'm calling makeFirstResponder on NSTextField's window with NSTextField as first responder.

    I tried different combination of running makeFirstResponder: before starting animation, in animation's completion handler, tried different variations of performSelector: call on main thread, used dispatch_async, dispatch_after, even performClick: (but this shrink-animates container view to 0, because any click outside closes it). The effect is always the same: there is focus on NSTextField - I can type in this text field, but initially there is no keyboard input cursor.

    When the editing is done, the makeFirstResponder on NSText which is chat's text input window cursor shows there properly.

    What am I missing there? Why text field gets focus without cursor? What can prevent NSTextField that is made first responder not to show cursor? I'm almost completely sure I'm not making any other control first responder afterwards anywhere in the app. Please help.


    After hours of experiments and internet browsing in search for solution I found this extremely useful hack:

    https://gist.github.com/Kapeli/7abd83d966957c17a827

    - (void)ensureCursorBlink 
    {
        if(isYosemite && !self.stringValue.length)
        {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if(!self.stringValue.length)
            {
                [self setStringValue:@" "];
                [self setStringValue:@""];
            }
            });
       }
    }
    

    and call it after makeFirstResponder:. Actually, it worked for me even without dispatch_after.

    链接地址: http://www.djcxy.com/p/85260.html

    上一篇: 当NSPopover打开时,macOS NSTextfield失去焦点

    下一篇: NSTextField获取焦点,但没有文本输入光标可见