我正在尝试将我在滑块上显示的数字值转到我的动作事件中,其中单击按钮并运行数学等式来查找提示。
SLIDER
@IBAction func percentageSlider(sender: UISlider) {
var sliderPercentage = lroundf(sender.value)
sliderValue.text = "\(sliderPercentage)%"
}
BUTTON(获取滑块百分比并计算它)
@IBAction func calculateTapped(sender: AnyObject) {
//1. get total bill
var userInput = billTextField.text as NSString
var totalBill : Float = userInput.floatValue
//2.Determine the tip value
var tipRate : Float = //HERE SHOULD BE WEHERE THE VALUE FROM THE SLIDER GOES
var indexString : String = "0.\(tipRate)"
var index = indexString.toInt()
//3. Calculate the tip
var tip : Float = totalBill * tipRate
//4. Display the tip
tipLabel.text = "$\(tip)"
}
如何让sliderPercentage在按钮功能中显示为变量?
答案 0 :(得分:0)
您需要创建一个滑块作为IBOutlet。 (将它在Interface Builder中拖到代码中)。之后,您将能够在课堂的任何地方访问它。
答案 1 :(得分:0)
这里试试这个,确保滑块的最小值= 1,最大值= 100.你可以在IB中做到这一点。并将插座连接到滑块
@IBOutlet weak var slider : UISlider
@IBAction func calculateTapped(sender: AnyObject) {
//1. get total bill
var userInput = billTextField.text as NSString
var totalBill : Float = userInput.floatValue
//2.Determine the tip value
var tipRate : Float = lroundf(slider.value)
var indexString : String = "0.\(tipRate)"
var index = indexString.toInt()
//3. Calculate the tip
var tip : Float = totalBill * tipRate
//4. Display the tip
tipLabel.text = "$\(tip)"
}