简体   繁体   中英

UISegmentedControl if statement

I am trying learning the UISegmentedControl and want to figure out how to get it to work within an if statement to return one value based on selection and another value based on the other selection. There is only two values the UISegment can return - "Fixed" or "Varibale". The code below does not work but gives an idea what I want. The two variables are predefined.

  @IBAction func calPenalty(_ sender: UIButton) {

    let startDate = termStartDate.text
    let mortgageTerm = Double(mortgageTermLabel.text!)
    let discount = Double(mortgageDiscountLabel.text!)
    let mtgCashback = Double(casbackRecieved.text!)
    let mtgBalance = Double(mortgageBalance.text!)
    let rate = Double(currentRate.text!)
    //let mortgageType = mortgageType

    let lender = Global.selectedRate
    if let oneYear = Double((lender?.oneYear)!),
        let twoYear = Double((lender?.twoYear)!),
        let threeYear = Double((lender?.threeYear)!),
        let fourYear = Double((lender?.fourYear)!),
        let fiveYear = Double((lender?.fiveYear)!) {

        let maturityDate = (getMaturityDate(startdate: startDate!, termMonths: Int(mortgageTerm!)))

        let monthsToMaturity = daysBetweenDates(endDate: maturityDate)/365*12

        let comparisonTerm = (IRDComparisonTerm(monthsToMaturity: monthsToMaturity))

        let cashback = cashbackRepayment(cashbackRecieved: mtgCashback!, mtgTerm: Double(mortgageTerm!), mthsToMaturity: Double(monthsToMaturity))

        print(cashback)


        var comparisonRate: Double = 0

        switch comparisonTerm
        {
        case 12:
            comparisonRate = oneYear

        case 24:
            comparisonRate =  twoYear

        case 36:
            comparisonRate =  threeYear
        case 48:
            comparisonRate = fourYear
        case 60:
            comparisonRate = fiveYear

        default:
            comparisonRate = 0

        } // end switch statement

       print(comparisonRate)

        let IRD = IRDPenalty(IRDComparisonRate: comparisonRate, mtgBalance: mtgBalance!, mthsToMaturity: Double(monthsToMaturity), discount: discount!, currentRate: rate!)

        let threeMthsInterestPenalty = threeMonthsInterestPenalty(mtgBalance: mtgBalance!, mtgRate: rate!)

        print (IRD)
        print (threeMthsInterestPenalty)


        var penalty: Double = 0
       // var totalPenalty: Double = 0

        if IRD > threeMthsInterestPenalty {

            penalty = IRD + cashback
        }else{

            penalty = threeMthsInterestPenalty + cashback
        }



      // totalPenalty = penalty + cashback

         calculationLabel.text = String(penalty)


    }

}
// triggers result based on value selected
   @IBAction func valueChanged(_ sender: UISegmentedControl) {

    switch sender.selectedSegmentIndex {
    case 0:
        calculationLabel.text = String(penalty)

    case 1:
        calculationLabel.text = String(threeMthsInterestPenalty + cashback)

    default:
         calculationLabel.text = String(penalty)
    }


}

Edit: I am still working on this problem. I have updated to the above code to show all the code within the IBAction. The @IBAction does not work as the variables are undefined based on where they are posted within the code. The Storyboard has a UIsegmentedControl with to values "Fixed and Variable. If the user selects fixed then I want it to show as per the IBAction for UISegement Control.

If what you want is to check when the value of your segmented control changes, and you want to do it programmatically, you firstly need to register a target-action method using valueChanged constant. Then, write up your valueChanged function. Check this out for more information.

import UIKit

class ViewController: UIViewController {

    // Reference from Storyboard or Interface Builder
    @IBOutlet weak var mortgageType: UISegmentedControl!

    // For set up a UISegmentedControl programmatically
    var segmentedControl: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func loadView() {
        super.loadView()

        // Programmatically setup
        let items = ["Cinnamon", "Clove"]
        segmentedControl = UISegmentedControl(items: items)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.frame = CGRect(x: 120, y: 200, width: 200, height: 30)
        segmentedControl.tintColor = UIColor.black
        segmentedControl.addTarget(self, action: #selector(self.valueChanged(_:)), for: .valueChanged)
        self.view.addSubview(segmentedControl)
    }

    @IBAction func valueChanged(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            print("Hi")

        case 1:
            print("Bye")

        default:
            print("Nothing")
        }
    }
}

If you reference your UISegmentedControl from Storyboard and Interface Builder. You should also add an action to it. 在此处输入图片说明

Update to the edited question

@IBAction func calPenalty(_ sender: UIButton) {
    if mortgageType.selectedSegmentIndex == 0 {
        print("Fixed")
    } else {
        print("Variable")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM