UIStepper練習|瘋狂購物APP

自己當老闆,到底要賣什麼好?

嗯,賣玩具好了!

玩具總動員可是陪伴著我長大的啊!這張圖涵蓋了全集出現的玩具們吶💗

📣做了分別兩種畫面: ViewController & TableViewController
只是單純想試試 TableViewController的畫面,其功能並不完整,但兩者原本預設功能皆相同😂。

圖左:完整app畫面.gif
圖中:ViewController畫面
圖右:TableViewController畫面

🌟畫面包含:
1. 建立IBOutlet:

・每個商品數量的 TextField (使用者能自行輸入數量)
・調整購買數量的UIStepper
・總金額Label

2. 建立IBAction:
・UIStepper.value 取得購買數量&顯示在label中 (全部使用同一個Action)
・calculate 計算購買之總金額
・clearAll 清除購買數量及總金額

- 設定scrollView
- 搭配Static Cells 設計storyboard頁面

1. 建立IBOutlet

2. 建立IBAction

UIStepper.value 取得購買數量&顯示在label中 (全部使用同一個Action)

全部使用同一個Action
//宣告設定玩具數量的初始值為0
var
itemJessie:Int = 0
var itemWoody:Int = 0
var itemBuzz:Int = 0
var itemRex:Int = 0
var itemHamm:Int = 0
@IBAction func changeQty(_ sender: UIStepper) {//取得stepper的值
itemJessie = Int(jessieStepper.value)
itemWoody = Int(woodyStepper.value)
itemBuzz = Int(buzzStepper.value)
itemRex = Int(rexStepper.value)
itemHamm = Int(hammStepper.value)
//並顯示在label中
jessieQtyTextField.text = "\(itemJessie)"
woodyQtyTextField.text = "\(itemWoody)"
buzzQtyTextField.text = "\(itemBuzz)"
rexQtyTextField.text = "\(itemRex)"
hammQtyTextField.text = "\(itemHamm)"
//計算總金額(看下一段了解)
calculate()
}

calculate() 計算購買之總金額

//將所有Stepper的 值*金額加總
func
calculate() {
let sum = 299 * itemJessie + 359 * (itemWoody + itemBuzz) + 259 * (itemRex + itemHamm)
//將金額顯示於totalLabel
totalLabel.text = "\(sum)"
}

clearAll 清除購買數量及總金額UIButton

//清除所有值:將所有stepper, textField值, totalLabel設為0@IBAction func clearAll(_ sender: UIButton) {
jessieStepper.value = 0
woodyStepper.value = 0
buzzStepper.value = 0
rexStepper.value = 0
hammStepper.value = 0
jessieQtyTextField.text = "0"
woodyQtyTextField.text = "0"
buzzQtyTextField.text = "0"
rexQtyTextField.text = "0"
hammQtyTextField.text = "0"
totalLabel.text = "0"
}

-設定ScrollView可參考此篇

在storyboard裡設定ScrollView功能,怕忘記趕快拿出來複習複習。

搭配Static Cells 設計storyboard頁面(參考此篇)

TableViewController畫面參照彼得潘的文章即可完成。

//務必刪除這段,否則將只顯示一片空白的表格!override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}

GitHub

--

--