我正在尝试学习 UISegmentedControl,并想弄清楚如何让它在 if 语句中工作,以根据选择返回一个值,并根据另一个选择返回另一个值。 UISegment 只能返回两个值 - “Fixed”或“Varibale”。下面的代码不起作用,但给出了我想要的想法。这两个变量是预定义的。
@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)
}
}
编辑:我仍在解决这个问题。我已经更新到上面的代码以显示 IBAction 中的所有代码。 @IBAction 不起作用,因为根据它们在代码中的发布位置 undefined variable 。 Storyboard 有一个 UIsegmentedControl,其值为“固定和可变”。如果用户选择固定,那么我希望它按照 UISegment 控件的 IBAction 显示。
Best Answer-推荐答案 strong>
如果您想要检查分段控件的值何时发生变化,并且您想以编程方式进行,您首先需要使用 valueChanged 常量注册一个目标操作方法。然后,编写您的 valueChanged 函数。查看 this了解更多信息。
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")
}
}
}
如果您从 Storyboard 和 Interface Builder 中引用您的 UISegmentedControl。您还应该向它添加一个操作。
更新已编辑的问题
@IBAction func calPenalty(_ sender: UIButton) {
if mortgageType.selectedSegmentIndex == 0 {
print("Fixed")
} else {
print("Variable")
}
}
关于ios - UISegmentedControl if 语句,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/44688512/
|