访问Swift中的其他函数
问题描述:
我想要将我在滑块值上显示的数字覆盖到我的操作事件中单击此按钮的位置,并运行搜索提示的数学公式。访问Swift中的其他函数
滑块
@IBAction func percentageSlider(sender: UISlider) {
var sliderPercentage = lroundf(sender.value)
sliderValue.text = "\(sliderPercentage)%"
}
按钮(注意到滑块百分比计算的话)
@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显示为在按钮功能的变量?
答
您需要创建一个滑块作为IBOutlet。 (在Interface Builder中将其拖到代码中)。之后,您将可以在课程中的任何地方访问它。
答
这里尝试这一点,确保滑块具有最小值= 1,最大= 100.you可以IB.And做到这一点连接插座上的滑块
@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)"
}
当我这样做我得到一个错误:( – user278063
什么样的错误 –
类AppDelegate:NSObject,UIApplicationDelegate {调试器突出显示此行,并说错误代码“SIGABRT”。它只显示启动屏幕,不会超过它 – user278063