if else練習|擇偶條件APP

聖誕節要到了,交換禮物買了嗎?

繼前篇作業瘋狂購物APP自己當老闆賣玩具後,又寫了一篇「擇條件」沒錯就是玩偶的🧸

btw, 現在每篇作業平均產速最少(真的是最少😭😭😭)要一天半才能產出難能可貴的一篇吶!每篇作業、每個APP都是記錄著我成長的愛的結晶 💖 看著自己能做出越來越像樣的功能畫面,滿心感謝努力不懈的自己,全年無休的彼得潘,還有學長姐的作業!!!!

交換禮物還想不到要送什麼嗎?
🎄聖誕禮物組合會是你不錯的選擇!
🎄

[ 此篇作業大量運用 if else &少數亂數運用 ]

🌟畫面包含:
1. 建立IBOutlet:
・顯示訂單訊息之TextView
・選擇品項之Segmented Control
・表現數量之Slider
・取件方式之Switch
・顯示Slider數值之Label

2. 建立IBAction:
・UISegmentedControl 設定切換品項名稱
・UISlider.value 設定slider的值顯示在label中
・UISwitch設定同時只能有一個Switch是開啟狀態(兩個Switch使用同一個Action)
・UIButton: 結帳鍵, 以文字顯示所有物件之成果
・UIButton: 清除鍵,將所有值設為0

1.建立IBOutlet

2. 建立IBAction

💡灰底的程式碼都是往左對齊,因此看起來沒那麼順,只是方便我增加/修改註解文字,如需美麗縮排程式碼請移駕文末GitHub

UISegmentedControl 設定切換品項名稱

@IBAction func giftSetSelection(_ sender: UISegmentedControl) {//如giftSegment分頁為0,品項名稱為Golden Set
if
giftSegment.selectedSegmentIndex == 0 {
giftSetstr = "Golden Set"
//如giftSegment分頁為1,品項名稱為Luxury Set
} else if giftSegment.selectedSegmentIndex == 1{
giftSetstr = "Luxury Set"
}
}

UISlider 設定slider的值顯示在label中

round() 為計算四捨五入後的整數

@IBAction func qtySlider(_ sender: UISlider) {qtySlider.value.round()
qtyLabel.text = String(qtySlider.value)
//設定顯示在textView中之數字(UIButton:CheckOut)
qtyValue = Int(qtySlider.value)
}

UISwitch 設定同時只能有一個Switch是開啟狀態 (兩個Switch使用同一個Action)

連動兩個Switch時,試了大半天,後來求助彼得潘大大,得知應須以if sender == deliverySwitch去預先判斷按的是哪一個開關,無法直接預設其Switch為if deliverySwitch.isOn

左圖:同時只有一個Switch是開啟狀態
中圖:Delivery開啟時顯示的文字
右圖:Pick UP 開啟時顯示的文字

@IBAction func deliveryOrNot(_ sender: UISwitch) {//由sender 預先判斷按的是哪一個開關
if sender == deliverySwitch{
//如deliverySwitch是開啟的,pickupSwitch就要關起來
if deliverySwitch.isOn {
pickupSwitch.setOn(false, animated: true)
//設定顯示在textView中之數字(UIButton:CheckOut)
deliveryOrNotstr = "deliver to your address."
}
//否則 如pickupSwitch是開啟的,deliverySwitch就要關起來
} else {
if pickupSwitch.isOn {
deliverySwitch.setOn(false, animated: true)
//設定顯示在textView中之數字(UIButton:CheckOut)
deliveryOrNotstr = "pick up in-store."
}
}
}

UIButton: 結帳鍵, 以文字顯示所有物件之成果

練習亂數,因此使用亂數決定是否缺貨(有貨也不一定要賣你😉)

左圖:缺貨時的畫面
中圖:未填寫物品數量的畫面
右圖:成功完成訂單時的畫面

//按下結帳鍵,將...
@IBAction func checkOutButton(_ sender: UIButton) {
//亂數決定是否缺貨
let isOutOfStock = Bool.random()
//如缺貨將顯示此段文字,此段文字顏色為紅色
if isOutOfStock {
orderInformationTextView.text = "Sorry! \n It's out of stock!"
orderInformationTextView.textColor = UIColor.red
}
//如slider數量為0
else if qtySlider.value == 0 {
//將顯示此段文字,此段文字為紅色,數量label為紅色
orderInformationTextView.text = "**Quantity needed!**"
orderInformationTextView.textColor = UIColor.red
qtyLabel.textColor = UIColor.red
//否則將顯示此段文字,此段文字為黑色,數量label為黑色
}else{
orderInformationTextView.text = "You order \(qtyValue) Christmas \(giftSetstr) and will \(deliveryOrNotstr) \n Thank you!"
orderInformationTextView.textColor = UIColor.black
qtyLabel.textColor = UIColor.black
}
}

UIButton: 清除鍵,將所有值設為0

//按下清除鍵,將所有值設為0
@IBAction func clearALLButton(_ sender: UIButton) {
qtySlider.value = 0
qtyLabel.text = "0"
giftSegment.selectedSegmentIndex = 0
deliverySwitch.setOn(false, animated: true)
pickupSwitch.setOn(false, animated: true)
orderInformationTextView.text = "ORDER INFORMATIONS"
orderInformationTextView.textColor = UIColor.black
qtyLabel.textColor = UIColor.black
}

GitHub

--

--