2016-03-31 5 views
0

フォームを検証するために使用するこのアクションボタンがありますが、このボタンを押すとアラートが表示され、アラートが終了すると(「OK」を押す)、メインにセグを実行しますVC。私は、ボタンを押した後:私が間違って何をやっているSwift:予期せずセグを実行する

@IBAction func loginButton(sender: AnyObject) { 

     if self.password.text == "" || self.email.text == "" { 

      self.displayAlert("Error", message: "Please insert email and password") 
      print("if") 

     } 


     } 


    func displayAlert(title: String, message: String) { 

     // cria a mensagem de alerta 
     var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 

     // adiciona botao a mensagem de alerta 
     alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in 

      //o que faz quando o botao da mensagem de alerta é apertado. 
      self.dismissViewControllerAnimated(true, completion: nil) 


     }))) 

     //apresenta a mensagem. 
     self.presentViewController(alert, animated: true, completion: nil) 



    } 

任意のアイデアを同じVCに滞在したいのですが? Ps。私はアウトレットをチェックして、彼らと一緒にトリックはありません。

答えて

2

この行削除:ユーザーがOKを押すと、アラートビューがすでに却下された後にこれがある

self.dismissViewControllerAnimated(true, completion: nil) 

が起こります。 ここで行ったことは、アラートを表示したビューを閉じることです。

+0

ありがとうございました!それは私のために働いた。 –

+0

あなたの歓迎です。そして正解とマークしてください – Roee84

1

このようなことを行う必要があります。

alert.addAction((UIAlertAction(title: "Ok", style: .Cancel, handler: nil)) 

OKを押した後に何かを実行する場合は、ハンドラでそれを行います。

関連する問題