2015-07-11 94 views
0

我想要將我在滑塊值上顯示的數字覆蓋到我的操作事件中單擊此按鈕的位置,並運行搜索提示的數學公式。訪問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顯示爲在按鈕功能的變量?

回答

0

您需要創建一個滑塊作爲IBOutlet。 (在Interface Builder中將其拖到代碼中)。之後,您將可以在課程中的任何地方訪問它。

0

這裏嘗試這一點,確保滑塊具有最小值= 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)" 

}

+0

當我這樣做我得到一個錯誤:( – user278063

+0

什麼樣的錯誤 –

+0

類AppDelegate:NSObject,UIApplicationDelegate {調試器突出顯示此行,並說錯誤代碼「SIGABRT」。它只顯示啓動屏幕,不會超過它 – user278063