macOS UI 选择! Radio Buttons

选择! Radio Buttons

Radio Button的功能机像名称所说的Radio(收音机)一样,每次只能选择一个频道收听,所以这也代表每一次只能选择一个选项。

新增 Radio Buttons

新增的 Radio Buttons 是以群组的关念存在的,一开始你必需要将Radio Group拖拉到Layout的画面上:

调整Radio数量

预设会使用Matrix方式建立2个Radio Buttons,但接下来你可以在Cells选项中的Rows(行)、Columns(列)输入您需要的数量:

输入完成后再来转换到Layout上面看一下Radio Buttons是否有变化:

编辑Radio内容

先前已经有设定成四个Radio Buttons,要变更Radio Buttons的文字内容时,在文字上用滑鼠指标连点两下就能进行编辑:

隐藏Radio

除了之前可以编辑Radio内容外,也可以针对单一个Radio Button来设定功能,像是如果不想要显示这一个Radio,可以将Visual选项中的Transparent 勾选就能将这个Radio隐藏:

选择Radio状态

承先前已经选择单一个Radio动作之后,Radio属性上也能选择它的预设状态:

状态可以选择:On、Off,另一个Mixed以及下面的Allows Mixed这个在Radio Buttons不需去考虑它。

程式侦测Radio Buttons

侦测Radio Buttons的行为必需要先宣告它的IBAction之后在Layout上与程式做个关联才能使用。

标头档(.h)宣告使用到的Action名称

//-----------start-----------
-(IBAction)actionRadioButtons:(id)sender;
//------------end------------

程式档(.m)

//-----------start-----------
-(IBAction)actionRadioButtons:(id)sender {
NSButtonCell *selCell = [sender selectedCell];
NSLog (@"Title cell is %@", selCell.title);
}
//------------end------------

Radio是属于NSButtonCell的其中一个类型,上面所传递的sender为其NSButtonCell Object

关联

将IBAction事先宣告完成时,点选Radio Buttons区块并按下滑鼠右键可以看到事件供选择,利用它来选择Send Actionsselect,将select右边小圈圈拉到App Delegate后,就能选择刚建立的actionRadiobuttons的method后完成连动。

上图为开始进行关联的滑鼠拖拉动作

选到App Delegate时,Xcode会去寻找未使用的IBAction宣告并将它列出来供选择,这只有宣告一组,会看到actionRadioButtons

完成后能看到上图有变化,这代表关联完成。

执行测试

先前的程式功能为选择Radio后将Radio的title显示,为了好识别,后续将Radio文字依序改成1、2、3、4,当你选择时就会印出来1、2、3、4: