-1
Good afternoon, you guys!
I’m trying to make an app and I can’t do the programming so it doesn’t go to the next field with the empty field. The way the code is left blank and press to calculate the system closes.
I believe that because of the conversion, because if I leave in a normal way the keyboard for being Portuguese does not accept the comma, then I had to do the conversion.
If any of you can help me, I’d appreciate it!
Below the code, complete.
import UIKit
class MediasViewController: UIViewController {
    @IBOutlet weak var Litros: UITextField!
    @IBOutlet weak var MKhini: UITextField!
    @IBOutlet weak var Mkhfin: UITextField!
    @IBOutlet weak var MresultadoKh: UITextField!
    @IBAction func btnMedia(_ sender: UIButton) {
        let LitrosGeral = Double(self.Litros.text!)!
        let formatter = NumberFormatter()
        func convertToDouble (_ string: String) -> Double {
            formatter.numberStyle = .none
            return formatter.number(from: string)!.doubleValue
        }
        formatter.usesGroupingSeparator = true
        let Mkhini = convertToDouble(MKhini.text!)
        let Mkhfina = convertToDouble(Mkhfin.text!)
        var resultadoMediakh = (Mkhini - Mkhfina) * 17.8 * LitrosGeral
        if resultadoMediakh / 79.8 < 0 {
            (resultadoMediakh = 0)
        } else {
            resultadoMediakh = (resultadoMediakh / 79.8) / 3
        }
        MresultadoKh.text = String (format: "%.1f", resultadoMediakh)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let alerta = UIAlertController (title: "Alerta", message: "teste", preferredStyle: .alert)
        let confirmar = UIAlertAction(title: "OK", style: .default, handler: nil)
        alerta.addAction(confirmar)
        present(alerta, animated: true, completion: nil)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}