選擇! 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 Actions
的select,將select右邊小圈圈拉到App Delegate
後,就能選擇剛建立的actionRadiobuttons
的method後完成連動。
上圖為開始進行關聯的滑鼠拖拉動作
選到App Delegate時,Xcode會去尋找未使用的IBAction宣告並將它列出來供選擇,這只有宣告一組,會看到actionRadioButtons
。
完成後能看到上圖有變化,這代表關聯完成。
執行測試
先前的程式功能為選擇Radio後將Radio的title顯示,為了好識別,後續將Radio文字依序改成1、2、3、4,當你選擇時就會印出來1、2、3、4: