刷新步进值
Refresh stepper value
我有 3 个分段控件(游戏名称),以及 3 个用于价格、数量、小计的文本字段。我还有 1 个步进器来增加或减少 1 到 5 之间的数量。
当我更改一个游戏的数量并更改选择另一个游戏时,我的问题出现了。假设我将 game1 数量增加到 5 并更改游戏。数量字段自动设置为 1。但是步进器中的“+”是不可选择的,当我在步进器中按“-”时,数量变为 4。
我想我需要的是在我从分段控制更改游戏时刷新步进器值。任何的想法 ?
class ViewController: UIViewController {
@IBOutlet weak var Photo: UIImageView!
@IBOutlet weak var seg_games: UISegmentedControl!
@IBOutlet weak var lbl_motto: UILabel!
@IBOutlet weak var txt_unitPrice: UITextField!
@IBOutlet weak var txt_quantity: UITextField!
@IBOutlet weak var txt_totalPrice: UITextField!
@IBOutlet weak var step_qChooser: UIStepper!
@IBAction func stepChanged(sender: UIStepper) {
if (txt_quantity.text.isEmpty) {
let alert = UIAlertView()
alert.title = "No Game Choose"
alert.message = "Please Pick Your Game First !"
alert.addButtonWithTitle("Ok")
alert.show()
}
else {
txt_quantity.text = Int(sender.value).description
txt_totalPrice.text = TotalCalc()
}
}
func TotalCalc () -> String {
var a = txt_unitPrice.text.toInt()
var b = txt_quantity.text.toInt()
var sonuc = a! * b!
return String(sonuc)
}
@IBAction func seg_games(sender: AnyObject) {
var index = seg_games.selectedSegmentIndex
var choice = seg_games.titleForSegmentAtIndex(index)!
if choice == "Fallout"
{
lbl_motto.text = "Be Prepare your P.I.M.P."
self.Photo.image = UIImage(named: "fallout.png")
txt_quantity.text = "1"
txt_unitPrice.text = "80"
txt_totalPrice.text = TotalCalc()
}
else if choice == "Borderlands"
{
lbl_motto.text = "Ready for BIG BOYS !!!"
self.Photo.image = UIImage(named: "borderlands.png")
txt_quantity.text = "1"
txt_unitPrice.text = "55"
txt_totalPrice.text = TotalCalc()
}
else if choice == "Battlefield"
{
lbl_motto.text = "Choose: Sniper or Assult"
self.Photo.image = UIImage(named: "battlefield.png")
txt_quantity.text = "1"
txt_unitPrice.text = "110"
txt_totalPrice.text = TotalCalc()
}
}
UIStepper
的value
既可读又可写。
如果您在更改分段时总是希望数量从 return 到 1
,则只需在更改分段时将步进器值设置为 1.0
:
step_qChooser.value = 1.0
如果你想让数量保持不变(例如,如果你离开并回来,Borderlands 的数量保持在 3
),你需要将每个项目的数量与 UIStepper
.
创建一个数组来保存步进值。这是您的 ViewController
的 属性,因此它应该定义在任何方法之上:
var quantities = [1.0, 1.0, 1.0]
这些数量将由您从 UISegmentedControl
获得的 index
编制索引。
选择新游戏后,根据数量更新步进器值并相应地设置文本字段:
step_qChooser.value = quantities[index]
txt_quantity.text = "\(Int(quantities[index]))"
当步进值发生变化时,请务必更新 quantities
数组中的值。在 stepChanged()
函数中执行:
quantities[seg_games.selectedSegmentIndex] = step_qChooser.value
我有 3 个分段控件(游戏名称),以及 3 个用于价格、数量、小计的文本字段。我还有 1 个步进器来增加或减少 1 到 5 之间的数量。
当我更改一个游戏的数量并更改选择另一个游戏时,我的问题出现了。假设我将 game1 数量增加到 5 并更改游戏。数量字段自动设置为 1。但是步进器中的“+”是不可选择的,当我在步进器中按“-”时,数量变为 4。
我想我需要的是在我从分段控制更改游戏时刷新步进器值。任何的想法 ?
class ViewController: UIViewController {
@IBOutlet weak var Photo: UIImageView!
@IBOutlet weak var seg_games: UISegmentedControl!
@IBOutlet weak var lbl_motto: UILabel!
@IBOutlet weak var txt_unitPrice: UITextField!
@IBOutlet weak var txt_quantity: UITextField!
@IBOutlet weak var txt_totalPrice: UITextField!
@IBOutlet weak var step_qChooser: UIStepper!
@IBAction func stepChanged(sender: UIStepper) {
if (txt_quantity.text.isEmpty) {
let alert = UIAlertView()
alert.title = "No Game Choose"
alert.message = "Please Pick Your Game First !"
alert.addButtonWithTitle("Ok")
alert.show()
}
else {
txt_quantity.text = Int(sender.value).description
txt_totalPrice.text = TotalCalc()
}
}
func TotalCalc () -> String {
var a = txt_unitPrice.text.toInt()
var b = txt_quantity.text.toInt()
var sonuc = a! * b!
return String(sonuc)
}
@IBAction func seg_games(sender: AnyObject) {
var index = seg_games.selectedSegmentIndex
var choice = seg_games.titleForSegmentAtIndex(index)!
if choice == "Fallout"
{
lbl_motto.text = "Be Prepare your P.I.M.P."
self.Photo.image = UIImage(named: "fallout.png")
txt_quantity.text = "1"
txt_unitPrice.text = "80"
txt_totalPrice.text = TotalCalc()
}
else if choice == "Borderlands"
{
lbl_motto.text = "Ready for BIG BOYS !!!"
self.Photo.image = UIImage(named: "borderlands.png")
txt_quantity.text = "1"
txt_unitPrice.text = "55"
txt_totalPrice.text = TotalCalc()
}
else if choice == "Battlefield"
{
lbl_motto.text = "Choose: Sniper or Assult"
self.Photo.image = UIImage(named: "battlefield.png")
txt_quantity.text = "1"
txt_unitPrice.text = "110"
txt_totalPrice.text = TotalCalc()
}
}
UIStepper
的value
既可读又可写。
如果您在更改分段时总是希望数量从 return 到 1
,则只需在更改分段时将步进器值设置为 1.0
:
step_qChooser.value = 1.0
如果你想让数量保持不变(例如,如果你离开并回来,Borderlands 的数量保持在 3
),你需要将每个项目的数量与 UIStepper
.
创建一个数组来保存步进值。这是您的
ViewController
的 属性,因此它应该定义在任何方法之上:var quantities = [1.0, 1.0, 1.0]
这些数量将由您从
UISegmentedControl
获得的index
编制索引。选择新游戏后,根据数量更新步进器值并相应地设置文本字段:
step_qChooser.value = quantities[index] txt_quantity.text = "\(Int(quantities[index]))"
当步进值发生变化时,请务必更新
quantities
数组中的值。在stepChanged()
函数中执行:quantities[seg_games.selectedSegmentIndex] = step_qChooser.value