NSPopUpButton,绑定和缩短使用寿命 [英] NSPopUpButton, Bindings and a shortening lifespan
问题描述
我想来了解使用NSPopUpButton有多么困难。这是远远的最困难的用户元素在Cocoa中编程(至少就我发现它)。
I am trying to come to grips with how difficult it is to use NSPopUpButton. It's by far and away the most difficult user element to program in Cocoa (at least as far as I am finding it).
我想到的用例是
- 我有一个名为Port的类,表示串行端口。
- 是名称字段。
- 在NSPopUpButton中,我要显示每个端口的名称字段。
- 当用户选择特定端口时,
- 当用户随后点击连接按钮时,我可以确定从数组中选择了哪个端口。
- 想要实现这个使用绑定,我想一旦我得到我的头,它将是更优雅的解决方案。
- I have a class called Port that represents a Serial port.
- Amongst the attributes is a name field.
- In the NSPopUpButton I want to display the name field for each port.
- When a user selects a specific port it is marked in the pop-up with a tick as expected
- When the user subsequently hits a connect button I can ascertain which of the Ports from the Array was selected.
- I would like to achieve this using bindings as I think once I get my head around it, it will be the more elegant solution.
在我的AppController.h我期待两个属性,我可以创建作为属性和综合:
Therefore in my AppController.h I am expecting two attributes which I can presumably create as properties and synthesize:
NSMutableArray *allPorts;
Port *currentlySelectedPort;
并在我的.m中执行一个操作:
and one action in my .m:
-(void)didSelectConnect:(id)sender{
NSLog(@"Selected port name is:%@",[currentlySelectedPort name]);
}
在Port.h中我有
NSString *name;
NSString *baudRate;
... etc ...
我创建了一个简单的项目,弹出(和标签)和各种文章,我已经设法填充一个NSMutableArray的元素,然后使用ArrayController显示值,然后选择设置标签的值(使用对象控制器)。然而,尽管这是聪明的,它不适合我试图实现的用例。所以我来这里寻求帮助
I have created a simple project which contains just a pop up (and a label) and following various articles, I have managed to populate an NSMutableArray with elements which I then use an ArrayController to display values and then on selection set the value of a label (using an object controller). However, as much as this is clever it doesn't fit the use case I am trying to implement. So I turn here for help
M
推荐答案
与 NSPopUpButton
有点复杂,因为它有两个需要的东西:一个绑定值和一个绑定,其中一个值被选择。更复杂的是,有一些完全合法的做法,你选择哪一个完全取决于你的程序的结构,在某种程度上,个人喜好。
OK, bindings with the NSPopUpButton
are a bit complicated because there are two things it needs: a binding for the values, and a binding for which one of those values is selected. What makes it even more complicated is that there are a couple of perfectly legitimate ways of doing that, and which one you choose entirely depends on your program's structure and, to some extent, personal preferences.
所以,为了得到一个值列表,你绑定 content
属性。在你的情况下,你可能绑定到 NSArrayController
的 arrangeObjects
键。在此设置中,每个菜单项表示一个对象。默认情况下,菜单项的标题是通过在数组中的每个项上调用 description
返回的字符串。如果要为菜单标题使用不同的属性,还可以绑定 contentValues
数组。只需确保为 contentValues
指定的键路径具有 content
作为其前缀的键路径(例如,您可以使用 arrangeObjects
for content
和 arrangedObjects.name
$ c> contentValues )
So, in order to get a list of values, you bind the content
property. In your case, you'd probably bind this to the arrangedObjects
key of an NSArrayController
. In this setup, each menu item represents one object. By default, the title of the menu item is the string returned by calling description
on each item in the array. If you want to use a different property for the menu title, you can also bind the contentValues
array. Just make sure the key path you specify for contentValues
has the key path for content
as its prefix (e.g. you might use arrangedObjects
for content
and arrangedObjects.name
for contentValues
)
这将给出代表对象的菜单项。接下来需要的是识别所选择的一种方法。您可以使用三种不同的绑定: selectedIndex
, selectedObject
和 selectedValue
。它们分别表示用户选择的数组对象的索引,对象值( content
数组中的对象之一)和所选择的字符串标题item( contentValues
数组中的一个对象,如果绑定了该属性)。
This will give you menu items that represent objects. What you need next is some way of identifying the selected one. There are three different bindings you can use: selectedIndex
, selectedObject
and selectedValue
. They represent, respectively, the index of the array object that the user selected, the object value (one of the objects in the content
array), and the string title of the selected item (one of the objects in the contentValues
array if you bound that property).
,您可以将 selectedObject
绑定到控制器类上的 selectedSerialPort
属性。当用户单击连接按钮时,您只需要引用 selectedSerialPort
属性。
So, in your case, you might bind selectedObject
to a selectedSerialPort
property on your controller class. When the user clicks the "Connect" button, you only have to refer to the selectedSerialPort
property.
这篇关于NSPopUpButton,绑定和缩短使用寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!