简体   繁体   English

将NSButton绑定到NSTextView中的选择

[英]Bind enabled of NSButton to selection in NSTextView

I have a button that does something to the selected text in NSTextView. 我有一个按钮,可以对NSTextView中的选定文本执行某些操作。 If nothing is selected then nothing happens when the button is pressed... so can the enabled property of a button be bound to whether or not some text is selected? 如果未选择任何内容,则在按下按钮时什么也不会发生...那么按钮的enabled属性是否可以绑定到是否选择了某些文本?

You don't want to bind a view to another view, anyway. 无论如何,您都不想将一个视图绑定到另一个视图。 The Right Way is to bind views to controllers, and Bindings gets cranky when you don't do things the Right Way. 正确的方法是将视图绑定到控制器,并且如果您未按照正确的方式进行操作,则绑定会变得胡思乱想。

Bind the button's enabled property to a property of the controller. 将按钮的enabled属性绑定到控制器的属性。 Have the controller be the delegate of the text view, and when the text view's selections change , update the controller's property accordingly. 让控制器作为文本视图的委托,并在文本视图的选择更改时 ,相应地更新控制器的属性。

If no text (was|is) selected, then the (old|new) selection ranges array will contain exactly one NSValue object, whose rangeValue will be a zero-length range. 如果未选择任何文本(||),则(旧|新)选择范围数组将仅包含一个NSValue对象,其rangeValue将为零长度范围。 This range is that of the insertion point; 该范围是插入点的范围; its length will be zero (nothing selected), but its location may not be (it will be wherever the insertion point is). 其长度将为零(未选择任何内容),但其位置可能不会为(它将位于插入点的任何位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM