刷新步进值

时间:2015-05-17 10:20:17

标签: swift uisegmentedcontrol uistepper

我有3个分段控件(游戏名称)和3个文本字段,用于价格,数量,小计。我还有1个步进器来增加或减少1到5之间的数量。

当我更改1个游戏的数量并更改选择其他游戏时,我的问题就出现了。让我们假设我将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()

            }

        }

1 个答案:

答案 0 :(得分:1)

value的{​​{1}}是可读写的。

如果您在更改细分时始终希望数量返回到UIStepper,则只需在更改细分时将步进器值设置为1

1.0

如果您希望数量保持不变(例如,如果您离开并返回,则Borderlands的数量保持在step_qChooser.value = 1.0 ),您需要将每个项目的数量与3分开存储。

  1. 创建一个数组来保存步进器值。这是UIStepper的属性,因此应在任何方法之上定义:

    ViewController

    这些数量将通过您从var quantities = [1.0, 1.0, 1.0] 获得的index编入索引。

  2. 选择新游戏后,请从数量中更新步进器值并相应地设置文本字段:

    UISegmentedControl
  3. 当步进器值更改时,请务必更新step_qChooser.value = quantities[index] txt_quantity.text = "\(Int(quantities[index]))" 数组中的值。在quantities函数内执行:

    stepChanged()