在viewDidLoad - Swift中观察步进器值的变化和调用功能

时间:2015-06-23 03:53:12

标签: swift ibaction viewdidload

我在ShowHideControl中定义了一个自定义类viewDidLoad()的变量,我想在步进器值发生变化时对同一个变量调用一个方法。我不确定如何在viewDidLoad内听取步进器值的变化。

ViewController.swift:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var player3EquityLbl: UILabel!

@IBAction func stepperChange() {
    stepperLabel.text = playerStepper.value.description

}

@IBOutlet weak var player4EquityLbl: UILabel!
@IBOutlet weak var playerStepper: UIStepper!
@IBOutlet weak var stepperLabel: UILabel!
@IBOutlet weak var totalBillLabel: UILabel!
@IBOutlet weak var winnerTxt: UILabel!

@IBOutlet weak var player1txt: UITextField!
@IBOutlet weak var player2txt: UITextField!
@IBOutlet weak var player3txt: UITextField!
@IBOutlet weak var player4txt: UITextField!

@IBOutlet weak var player1EquityLbl: UILabel!
@IBOutlet weak var player2EquityLbl: UILabel!

@IBOutlet weak var player1EquityWord: UILabel!

@IBOutlet weak var player2EquityWord: UILabel!
@IBOutlet weak var player3EquityWord: UILabel!
@IBOutlet weak var player4EquityWord: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    var playerTxtArray = [player1txt, player2txt, player3txt, player4txt]
    var playerEquityLblArray = [player1EquityLbl, player2EquityLbl, player3EquityLbl, player4EquityLbl]
    var playerEquityWordArray = [player1EquityWord,player2EquityWord,player3EquityWord,player4EquityWord]


    // initialize text boxes with zero value
    var playerBillVlaues  = PlayerBillValues(PlayerTextFieldArray: playerTxtArray)

    playerStepper.value = 2

    var showHideControl = ShowHideControl(lblArray:playerEquityLblArray, lblArray2: playerEquityWordArray, txtArray: playerTxtArray, playerStepper: playerStepper)

    showHideControl.showQuantity(2)

需要注意步进更改(我在这里称为假功能,以说明我需要做什么):

onStepperChange(showHideControl.showQuantity(playerStepper.value))

}

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

ShowHideControl.swift:

import Foundation

import UIKit

class ShowHideControl {

    var txtArray : [UITextField!] = []
    var lblArray : [UILabel!] = []
    var lblArray2 : [UILabel!] = []
    var totalShowing : Int = 0
    var playerStepper : UIStepper!

    init (lblArray : [UILabel!], lblArray2 : [UILabel!], txtArray: [UITextField!], playerStepper : UIStepper!){
        self.lblArray = lblArray
        self.lblArray2 = lblArray2
        self.txtArray = txtArray
        self.playerStepper = playerStepper

        showQuantity(2)

    }

    func showQuantity(let showNumber : Int){

        //show the correct number of labels and texts
        for var i = 0; i < showNumber-1; ++i {

            lblArray[i].hidden = false
            lblArray2[i].hidden = false
            txtArray[i].hidden = false
        }

        //hide the others
        for var i = showNumber; i < 4; ++i {
            lblArray[i].hidden = true
            txtArray[i].hidden = true
            lblArray2[i].hidden = true
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用委托来完成此任务(我只介绍需要更改的内容):

首先为<formname="A" action="WebPage.php" method="POST"> <input type="checkbox" name="Aa" onchange="update()" value="Aa"/>Aa <script type="text/javascript"> function update(){ var original = document.getElementById('Aa'); var other = document.getElementById('Ba'); original.checked = other.checked; } </script> </form> <formname="B" action="WebPage.php" method="POST"> <input type="checkbox" name="Ba" value="Ba"/>Ba </form>

创建委托
ShowHideControl

然后,从protocol ShowHideControlDelegate{ func StepperChanged(stepper_value:Int) } class ShowHideControl { var delegate:ShowHideControlDelegate! init (lblArray : [UILabel!], lblArray2 : [UILabel!], txtArray: [UITextField!], playerStepper : UIStepper!){ self.delegate.StepperChanged(playerStepper.value) } } 类中捕获它:

ViewController