Search code examples
iosswiftxcodeswift3uialertcontroller

Reload View after UIAlertController


I have a signup screen with a phone number field. If the user type a too short phone number, I display an UIAlertController. After she dismisses the Alert, the user can't send her phone number again as the send button is tapped. Is there a way to reload the view when the alert is dismissed to make the button untapped? Thanks

    func present_alert(title:String, content : String){
    let myAlert = UIAlertController(title:title, message:content, preferredStyle:UIAlertControllerStyle.alert)
    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:nil)
    myAlert.addAction(okAction)
    self.present(myAlert, animated:true)
}

Solution

  • the main reason on button click you disabled the yourbutton sender.isEnabled = false, when ever the alert is presented again enable your button , surely works addyourbuttonName.isEnabled = true

    if you want to clear the current textfield value then use yourtextfield.text = ""

    let alertController = UIAlertController(title: “Simple”, message: “Simple alertView demo with Cancel and Ok.”, preferredStyle: UIAlertControllerStyle.alert)
    
    let okAction = UIAlertAction(title: “OK”, style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
    print(“OK”)
     yourtextfield.text = ""
     yourtextfield.becomeFirstResponder()
     addyourbuttonName.isEnabled = true
    }
    
    
    alertController.addAction(okAction)
    self.present(alertController, animated: true, completion: nil)