mardi 4 décembre 2018

UIswitch change calcul in button with if statement

I am trying to use a UIswitch button to change my calcul in a formula. Here is how it works, I have an app to sell things so if the user wants to add an option on his sale such as warranty or insurance, he has to click the UIswitch. If it is on there is a textfield which appears and he entered the number of months for warranty and insurance then the formula calcul the total price. So I managed to make the UIswitch to appear or disappear if on or of. What I don't understand is how to change the formula according if on or off. I try to do a if statement according the textfield is hidden or not but it doesn't work.

@IBAction func switchWarranty(_ sender: UISwitch) {
    if (sender.isOn == true){
        textFieldWarranty.isHidden = false



    }else{
        textFieldWarranty.isHidden = true
        textFieldWarranty.text = ""


    }
}

 @IBAction func switchInsurance(_ sender: UISwitch) {
    if (sender.isOn == true){

        textFieldInsurance.isHidden = false

    }else{

        textFieldInsurance.isHidden = true
        textFieldInsurance.text = ""

    }
}

@IBAction func test(_ sender: Any) {


    if let price1 = Float (textFieldPrice1.text!){
    if let warranty = Float (textFieldWarranty.text!){
    if let insurance = Float (textFieldInsurance.text!){
    if let price2 = Float (TextFieldPrice2.text!){

if textFieldWarranty.isHidden == false && textFieldInsurance.isHidden == false {


let  Total = ((price1 * (warranty * 0.03748)) * (insurance * 0.4729)) / price2

    Label.text = "\(Total)) prix rentable"



        } else if textFieldWarranty.isHidden == false && textFieldInsurance.isHidden == true {

let  Total = (price1 * (warranty * 0.03748) / (price2 * 0.115)

    Label.text = "\(Total)) prix garantie"


        } else if textFieldWarranty.isHidden == true && textFieldInsurance.isHidden == false {

let  Total = ((price1 * (insurance * 0.4729)) / price2

    Label.text = "\(Total)) prix assure"



        } else if textFieldWarranty.isHidden == true && textFieldInsurance.isHidden == true {

let  Total = price 1 / price2

    Label.text = "\(Total)) prix exclus"



        } else{
            Label.text = "erreur"

        }

Aucun commentaire:

Enregistrer un commentaire