1 menjawab

Jawaban Terbaik

Panggilan Anda ke createPin harus di dalam pengendali tindakan untuk okAction. Seperti yang Anda miliki sekarang, secondPinNumber akan dipanggil sebelum lansiran ditampilkan, jadi lansiran akan kosong atau nil, bergantung pada cara Anda menginisialisasinya.

IBAction func createData(_ sender: Any) {
    let enterPinAlertController = UIAlertController(title: "Enter New PIN", message: "", preferredStyle: .alert)
    enterPinAlertController.addTextField{ (textField1:UITextField)->Void in
        textField1.placeholder = "Enter PIN"
        textField1.isSecureTextEntry = true
    }
    enterPinAlertController.addTextField{   (textField2:UITextField)->Void in
        textField2.placeholder = "Re-Enter PIN"
        textField2.isSecureTextEntry = true
    }
    let okAction = UIAlertAction(title: "OK", style: .cancel)   {(action) in
        if let textFields = enterPinAlertController.textFields,
           let firstPinNumber = textFields[0].text,
           let secondPinNumber = textFields[1].text,
           firstPinNumber == secondPinNumber {
               createPIN(pinNum: secondPinNumber)
            } else {
               print ("PINs dont match!")
               let pinsDontMatchAlertController = UIAlertController(title: "PINs don't match!", message: "Try again", preferredStyle: .alert)
               let okAction = UIAlertAction(title: "OK", style: .cancel)  
               pinsDontMatchAlertController.addAction(okAction)
               self.present(pinsDontMatchAlertController, animated: true, completion: nil)
            } 
        }
    }

    enterPinAlertController.addAction(okAction)
    self.present(enterPinAlertController, animated: true, completion: nil)

}
1
Paulw11 15 Maret 2019, 01:22