UIswitch使用if语句更改按钮中的计算

时间:2018-12-04 16:57:17

标签: if-statement button uiswitch

我正在尝试使用UIswitch按钮来更改公式中的计算。这是它的工作方式,我有一个可以销售东西的应用程序,因此,如果用户要在其销售中添加一个选项(例如保修或保险),则必须单击UIswitch。如果在该字段上,则出现一个文本字段,并且他输入了保修和保险的月数,然后该公式计算总价格。所以我设法使UIswitch出现或消失。我不明白的是如何根据打开或关闭来更改公式。 我尝试根据文本字段执行if语句是否隐藏,但不起作用。

@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"

        }

0 个答案:

没有答案