0

テキスト フィールドに入力しているユーザーに有効な構文を示す IntelliSense スタイルのポップオーバーの作成に取り組んでいます。ユーザーが textField に引き続き入力できるように、フォーカスを与えずに NSPopover を表示する方法を知っている人はいますか? ポップオーバーは controlTextDidChange によってトリガーされます。

- (void) controlTextDidChange:(NSNotification *)obj
{

    NSTextField *field = [obj object];
    NSString *command = [field stringValue];

    if ([[command substringFromIndex: command.length - 1] isEqualToString: @"#"]){
        CompletionMenuController *completionController = [[CompletionMenuController alloc] initWithNibName: @"CompletionMenuController" bundle:[NSBundle mainBundle]];
        completionMenuPopover = [[NSPopoverInformation alloc] init];
        [completionMenuPopover setContentViewController: completionController];
        [completionMenuPopover setContentSize: completionController.view.frame.size];
        [completionMenuPopover setBehavior: NSPopoverBehaviorTransient];
        [completionMenuPopover setAppearance: NSPopoverAppearanceHUD];
        [completionMenuPopover showRelativeToRect:[_commandBar frame] ofView:_commandBar preferredEdge:NSMaxYEdge];
    }
}
4

2 に答える 2

1

NSPopover の自動終了は、フォーカスの変化の検出に依存しているようです。つまり、一時的なものである場合は、ファーストレスポンダーのステータスをポップオーバーに設定する必要があります。NSPopoverBehaviorApplicationDefinedフォーカスの問題が解決するかどうか試してみてください。ただし、ポップオーバーを閉じるように注意する必要があります。ただし、他に方法がない場合は、明示的にフォーカスを編集コントロールに戻すことも可能です。ポップオーバーを表示しても視覚的な外観は変わらないため、その短いフォーカス スイッチのちらつきはありません。

于 2014-01-16T08:50:11.913 に答える
1

これは回避策であり、これを達成しようとしていた方法とは異なりますが、機能します。ポップオーバーを開く前にカーソル位置をテキストフィールドに保存してから、テキストフィールドにファーストレスポンダーを指定し、カーソル位置を元の位置に戻します。

if ([_commandBar stringValue].length > 0){
    NSString *command   = [_commandBar stringValue];
    NSRange range       = [[_commandBar currentEditor] selectedRange];

    //Open popover if command is being typed
    if ([[command substringFromIndex: command.length - 1] isEqualToString: @"#"]){
        CompletionMenuController *completionController = [[CompletionMenuController alloc] initWithNibName: @"CompletionMenuController" bundle:[NSBundle mainBundle]];

        //Configure and Open Popover
        if ([completionMenuPopover isShown]) [completionMenuPopover close];
        completionMenuPopover = [[NSPopover alloc] init];
        [completionMenuPopover setContentViewController: completionController];
        [completionMenuPopover setContentSize: completionController.view.frame.size];
        [completionMenuPopover setBehavior: NSPopoverBehaviorTransient];
        [completionMenuPopover setAppearance: NSPopoverAppearanceHUD];
        [completionMenuPopover setAnimates: NO];
        [completionMenuPopover showRelativeToRect:[_commandBar frame] ofView:_commandBar preferredEdge:NSMaxYEdge];

        //Reset Command Bar as First Responder
        [_commandBar becomeFirstResponder];
        [[_commandBar currentEditor] setSelectedRange: range];
    }
}
于 2014-01-16T19:27:58.173 に答える