我正在尝试学习UISegmentedControl,并想知道如何让它在if语句中工作,以便根据选择返回一个值,并根据另一个选择返回另一个值。UISegment只能返回两个值-“固定”或"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不工作,因为这些变量是根据它们在代码中的发布位置而未定义的。Storyboard有一个to值“固定和变量”的UIsegmentedControl。如果用户选择“固定”,那么我希望它按照IBAction for UISegement控件显示。
发布于 2017-06-22 01:49:48
如果您想要的是检查分段控件的值何时发生变化,并且希望以编程方式进行更改,那么首先需要使用valueChanged常量注册一个目标操作方法。然后,编写您的valueChanged函数。有关更多信息,请查看这。
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引用您的UISegmentedControl。您还应该向它添加一个操作。

更新到已编辑的问题
@IBAction func calPenalty(_ sender: UIButton) {
if mortgageType.selectedSegmentIndex == 0 {
print("Fixed")
} else {
print("Variable")
}
}https://stackoverflow.com/questions/44688512
复制相似问题