2016-02-04 73 views
-1

我已經做了大量的搜索,但沒有找到我的問題的答案。Swift - UITextField重置但UILabel不會

我的兩個UITextFields字段正在重置使用清除功能。 UILabel保留了printWatts函數的原始值,不清除。希望有任何建議來解決這個小問題,因爲我學習Swift。謝謝!

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var inputFeet: UITextField! 
    @IBOutlet weak var inputWatts: UITextField! 
    @IBOutlet weak var resultsLabel: UILabel! 

    var stripFeet = "" 
    var wattValue = "" 
    var totalWatts : Float = 0.0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func submitButton(sender: AnyObject) { 
     calculateWatts() 
    } 

    @IBAction func clearButton(sender: AnyObject) { 
     clear() 
    } 

    func calculateWatts() { 
     if let stripFeet = inputFeet.text, 
      wattValue = inputWatts.text, 
      fstripFeet = Float(stripFeet), 
      fwattValue = Float(wattValue){ 
       totalWatts = fstripFeet * fwattValue 
     } 
     printWatts() 
    } 

    func printWatts() { 
     let formatWatts = String(format: "%0.2f", totalWatts) 
     resultsLabel.text = "Total watts: \(formatWatts)" 
    } 

    func clear(){ 
     inputFeet.text = "" 
     inputWatts.text = "" 
     self.resultsLabel.text = "" 
    } 
} 
+0

你能告訴我們你的按鈕連接的截圖嗎? – Eendje

+0

就是這樣!我以某種方式將提交和清除操作連接到我的提交按鈕。衛生署! – Boomspot

回答

0

感謝@Eendje建議我檢查我的連接。我有提交和清除操作都連接到我的提交按鈕。選項拖動太方便了。現在都好。