ios - Attempt to present UIAlertController whose view is not in the window hierarchy (Swift 3/Xcode 8) -


i'm trying create app , want show alert when there login error or if user forget enter username and/or password. however, warning:

warning: attempt present on view not in window hierarchy!

i have tried other solutions found here still can't fix it. here's code:

func createalert(title: string, message: string) {      let alert = uialertcontroller(title: title, message: message, preferredstyle: uialertcontrollerstyle.alert)      alert.addaction(uialertaction(title: "ok", style: .default, handler: { (action) in          self.dismiss(animated: true, completion: nil)      }))      self.present(alert, animated: true, completion: nil)  }  @ibaction func signinpressed(_ sender: any) {      if usernametextfield.text == "" || passwordtextfield.text == "" {          createalert(title: "error in form", message: "please enter email , password.")      } else {          var activityindicator = uiactivityindicatorview()          activityindicator = uiactivityindicatorview(frame: cgrect(x: 0, y: 0, width: 50, height: 50))         activityindicator.center = self.view.center         activityindicator.activityindicatorviewstyle = uiactivityindicatorviewstyle.gray         view.addsubview(activityindicator)         activityindicator.startanimating()         uiapplication.shared.beginignoringinteractionevents()          pfuser.loginwithusername(inbackground: usernametextfield.text!, password: passwordtextfield.text!, block: { (user, error) in              activityindicator.stopanimating()             uiapplication.shared.endignoringinteractionevents()              if error != nil {                  var displayerrormessage = "please try again later."                  let error = error nserror?                  if let errormessage = error?.userinfo["error"] as? string {                      displayerrormessage = errormessage                  }                  self.createalert(title: "sign in error", message: displayerrormessage)               } else {                  print("logged in")                  self.performsegue(withidentifier: "tosignin", sender: self)              }           })      }  } 

update: here's whole view controller

class viewcontroller: uiviewcontroller {  @iboutlet var usernametextfield: uitextfield! @iboutlet var passwordtextfield: uitextfield!  func createalert(title: string, message: string) {      let alert = uialertcontroller(title: title, message: message, preferredstyle: uialertcontrollerstyle.alert)      alert.addaction(uialertaction(title: "ok", style: .default, handler: { (action) in          self.dismiss(animated: true, completion: nil)      }))      self.present(alert, animated: true, completion: nil)  }  @ibaction func signinpressed(_ sender: any) {      if usernametextfield.text == "" || passwordtextfield.text == "" {          createalert(title: "error in form", message: "please enter email , password.")      } else {          var activityindicator = uiactivityindicatorview()          activityindicator = uiactivityindicatorview(frame: cgrect(x: 0, y: 0, width: 50, height: 50))         activityindicator.center = self.view.center         activityindicator.activityindicatorviewstyle = uiactivityindicatorviewstyle.gray         view.addsubview(activityindicator)         activityindicator.startanimating()         uiapplication.shared.beginignoringinteractionevents()          pfuser.loginwithusername(inbackground: usernametextfield.text!, password: passwordtextfield.text!, block: { (user, error) in              activityindicator.stopanimating()             uiapplication.shared.endignoringinteractionevents()              if error != nil {                  var displayerrormessage = "please try again later."                  let error = error nserror?                  if let errormessage = error?.userinfo["error"] as? string {                      displayerrormessage = errormessage                  }                  self.createalert(title: "sign in error", message: displayerrormessage)               } else {                  print("logged in")                  self.performsegue(withidentifier: "tosignin", sender: self)              }           })      }  }  override func viewdidappear(_ animated: bool) {       if pfuser.current() != nil {          performsegue(withidentifier: "tosignin", sender: self)      }      self.tabbarcontroller?.tabbar.ishidden = true  }  override func viewdidload() {     super.viewdidload()  } 

change createalert method this,

func createalert(title: string, message: string, controller: uiviewcontroller) {          let alert = uialertcontroller(title: title, message: message, preferredstyle: .alert)          alert.addaction(uialertaction(title: "ok", style: .default, handler: { (action) in              self.dismiss(animated: true, completion: nil)          }))         controller.present(alert, animated: true, completion: nil)      } 

and create alerts this,

self.createalert(title: "sign in error", message: "please try again later.", controller: self) 

this might solve problem.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -