2017-09-10 9 views
0

アプリケーションを作成しようとしていますが、ログインエラーが発生した場合、またはユーザーが入力を忘れた場合に警告を表示します。ユーザー名および/またはパスワード。ビューがウィンドウ階層にないUIAlertControllerを表示しようとしました(Swift 3/Xcode 8)

警告:しかし、私はいつもこの警告を取得しようとしましたが、そのビューウィンドウ 階層ではありません 上に存在します!

私がここで見つけた他の解決策を試しましたが、まだ解決できません。

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 an email and 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 as 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:ここでは全体のビューコントローラ

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 an email and 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 as 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() 

} 
+0

おそらく、 'viewDidLoad'または他の場所で' createAlert'を呼び出すでしょう。 View Controller全体のコードを追加してください。 –

+0

@PranavKasetti私は全体のビューコントローラコード – acoustickat

+0

okで投稿を更新しました。どのView ControllerをSegueでナビゲートしていますか? –

答えて

0

はまずUIAlertControllerに、このような属性を作成するためにこのコードを試してみてください。

var alertController: UIAlertController? 

そして、あなたはのviewDidLoad(でこれを追加する必要があります)、このような:あなたはsignInButton、ログインを押したときに

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.alertController = UIAlertController(title: "Alert", message: "Not images yet", preferredStyle: .alert) 
    self.alertController?.addAction(UIAlertAction(title: "Close", style: .default)) 
    view.addSubview((alertController?.view)!) 

} 

だから、あなたが呼び出す必要がありますが正しくありません。

@IBAction func signInPressed(_ sender: Any) { 

if usernameTextField.text == "" || passwordTextField.text == "" { 

    createAlert(title: "Error in form", message: "Please enter an email and 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 { 

      self.presentedViewController?.present(self.alertController!, animated: true, completion: nil) 
     } 
} 
0

変更これまでごcreateAlert方法だし、

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) 

    } 

そして、このようなアラートを作成、

ここに私のコードです
self.createAlert(title: "Sign in error", message: "Please try again later.", controller: self) 

これにより、問題が解決する場合があります。

+0

ありがとう!私のデバッグ領域に同じ警告が表示されます: – acoustickat

0

スウィフト3

func displayMyAlertMessageError(userMessage:String, titleHead: String) 
    //define displyMyAlertMessage. 
{ 
    let MyAlert = UIAlertController(title: titleHead, message: userMessage, preferredStyle:UIAlertControllerStyle.alert); 
    let okAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil);MyAlert.addAction(okAction); 
    self.present(MyAlert,animated:true,completion:nil); 
} 

@IBAction func signInPressed(_ sender: Any) { 

    if (usernameTextField.text?.isEmpty) || (passwordTextField.text?.isEmpty) 
    { 
     createAlert(title: "Error in form", message: "Please enter an email and password.") 
    } 
    else 
    { 
     //Your code here 
    } 
関連する問題