Watch for stepper value change and call function within viewDidLoad - Swift

778 views Asked by At

I have defined a variable of custom class ShowHideControl in viewDidLoad(), and I want to call a method on the same variable when there is a change in a stepper value. I'm not sure how to listen for the stepper value change within 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)

Need to watch for stepper change (I called a fake function here to illustrate what I need to do):

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

There are 1 answers

4
abinop On

You may accomplish this using a delegate (I am only presenting what needs to be changed):

First create the delegate for the ShowHideControl class

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)
    }

}

then, capture it from the ViewController class:

class ViewController: UIViewController, ShowHideControlDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

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


        showHideControl.delegate = self


    }


    func StepperChanged(stepper_value:Int){
        println("hey, we got the changed value!")
    }
}