Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NSTableCellView中的NSTextField-结束对焦点丢失的编辑_Macos_Cocoa_Nstableview_Nstextfield_Nscontrol - Fatal编程技术网

Macos NSTableCellView中的NSTextField-结束对焦点丢失的编辑

Macos NSTableCellView中的NSTextField-结束对焦点丢失的编辑,macos,cocoa,nstableview,nstextfield,nscontrol,Macos,Cocoa,Nstableview,Nstextfield,Nscontrol,我有一个基于视图的NSTableView视图(它本身有一个带有单个文本字段的单元格视图),以及tableview之外的一些按钮和文本字段。其中一个按钮将对象添加到tableview的数据源中,并在将行插入tableview后立即使其可编辑 如果用户输入文本并按下返回键,我将收到-(BOOL)控件:(NSControl*)控件文本shouldenediting:(NSText*)字段编辑器委托方法,我可以运行验证并保存值。但是,如果用户选择tableview之外的任何其他按钮或文本字段,则不会调用

我有一个基于视图的NSTableView视图(它本身有一个带有单个文本字段的单元格视图),以及tableview之外的一些按钮和文本字段。其中一个按钮将对象添加到tableview的数据源中,并在将行插入tableview后立即使其可编辑

如果用户输入文本并按下返回键,我将收到
-(BOOL)控件:(NSControl*)控件文本shouldenediting:(NSText*)字段编辑器
委托方法,我可以运行验证并保存值。但是,如果用户选择tableview之外的任何其他按钮或文本字段,则不会调用委托


检测NSTableCellView中文本字段焦点丢失的最佳方法是什么,这样我就可以在tableview条目上运行一些验证代码

如果我理解正确,您希望在以下情况下发出
控件:text应取消编辑:
通知:

  • 将新对象添加到阵列控制器
  • 将自动选择表中表示对象的行
  • 您可以通过编程方式选择相关行中的文本字段进行编辑
  • 用户立即(即不在文本字段中进行任何编辑)将焦点交给UI中其他位置的控件
  • 我过去使用过的一种方法是,在文本字段可供用户编辑之前,对与文本字段关联的字段编辑器进行一个无关紧要的编程更改。下面的代码片段显示了如何执行此操作-这是上述场景中的步骤2/步骤3

    func tableViewSelectionDidChange(notification: NSNotification) {
        if justAddedToArrayController == true {
            // This change of selection is occurring because the user has added a new
            // object to the array controller, and it has been automatically selected 
            // in the table view. Now need to give focus to the text field in the 
            // newly selected row...
    
            // Access the cell
            var cell = tableView.viewAtColumn(0,
                row: arrayController.selectionIndex,
                makeIfNecessary: true) as NSTableCellView
    
            // Make the text field associated with the cell the first responder (i.e. 
            // give it focus)
            window.makeFirstResponder(cell.textField!)
    
            // Access, then 'nudge' the field editor - make it think it's already
            // been edited so that it'll fire 'should' messages even if the user 
            // doesn't add anything to the text field
            var fe = tableView.window?.fieldEditor(true, forObject: cell.textField!)
            fe!.insertText(cell.textField!.stringValue)
        }
    }