输入值后在视图中隐藏键盘?

时间:2019-01-08 22:01:53

标签: ios swift

我有一个内置在3个不同视图中的小型计算器。在所有计算器上,我都希望键盘在点击屏幕上的任意位置或按按钮计算值时消失。 Ive研究了几种资源,并且对如何实现任何代码感到困惑。我的计算器代码如下。预先感谢!

class Calculators: UIViewController {

    @IBOutlet var fluidIn: UITextField!
    @IBOutlet var timeIn: UITextField!
    @IBOutlet var dripIn: UITextField!
    @IBOutlet var dripResult: UILabel!

    @IBOutlet var weight: UITextField!
    @IBOutlet var etomidate: UILabel!
    @IBOutlet var succ: UILabel!
    @IBOutlet var roc: UILabel!
    @IBOutlet var ketamine: UILabel!

    @IBOutlet var lbsIn: UITextField!
    @IBOutlet var KgIn: UITextField!
    @IBOutlet var KgOut: UILabel!
    @IBOutlet var LbsOut: UILabel!

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

    @IBAction func calculate(sender: UIButton) {

        let Number2 = Double(weight.text!)
        let etommult = (0.3)
        let etomresult = Number2! * etommult
        etomidate.text = "\(etomresult)"

        etomidate.text = NSString(format:"%2.1f mg",etomresult)as String;

        let Number3 = Double(weight.text!)
        let succmult = (2.0)
        let succresult = Number3! * succmult
        succ.text = "\(succresult)"

        succ.text = NSString(format: "%2.1f mg",succresult)as String;

        let Number4 = Double(weight.text!)
        let rocmult = (1.0)
        let rocresult = Number4! * rocmult
        roc.text = "\(rocresult)"

        roc.text = NSString(format: "%2.1f mg",rocresult)as String;

        let Number5 = Double(weight.text!)
        let fentmult = (2.0)
        let fentresult = Number5! * fentmult
        ketamine.text = "\(fentresult)"

        ketamine.text = NSString(format: "%2.1f mg",fentresult)as String;

    }

    @IBAction func KgConv(_ sender: Any) {

       let kg = Double(lbsIn.text!)
       let kgmult = (2.2)
       let kgresult = kg! / kgmult
       KgOut.text = "\(kgresult)"

       KgOut.text = NSString(format: "%2.1f kg",kgresult)as String;

    }

    @IBAction func LbsConv(_ sender: Any) {

        let lbs = Double(KgIn.text!)
        let lbsmult = (2.2)
       let lbsresult = lbs! * lbsmult
    LbsOut.text = "\(lbsresult)"

    LbsOut.text = NSString(format: "%2.1f kg",lbsresult)as String;
    }

    @IBAction func dripCalc(_ sender: Any) {

        let cc = Double(fluidIn.text!)
        let min = Double(timeIn.text!)
        let fct = Double(dripIn.text!)
        let dripRes = (cc! * fct!) / min!

        dripResult.text = "\(dripRes)"
        dripResult.text = NSString(format: "%2.1f gtts/min",dripRes)as String;

    }

1 个答案:

答案 0 :(得分:0)

无论何时何地,您都想在viewController中隐藏键盘时调用以下代码。

view.endEditing(true)