2016-04-18 23 views
0

私は、パスワードの文字数が正しくない場合には、ユーザーに続行させるべきではない警告コントローラを使ってログインアプリを作っています。正しい金額を入力すると、アラートコントローラがポップアップし、処理を続行できなくなります。私のコードではいけないものがありますか?UIAlertControllerが表示されないようにしてください

func alertDisplay() { 

    let alertController = UIAlertController(title: "Alert", message: "Five characters or more is required to login", preferredStyle: UIAlertControllerStyle.Alert) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) {ACTION -> Void in 
     // Does nothing 
    } 
    let okAction = UIAlertAction(title: "OK", style: .Default) { action -> Void in 
     // does nothing also 
    } 
    alertController.addAction(cancelAction) 
    alertController.addAction(okAction) 

    self.presentViewController(alertController, animated: true, completion: nil) 
    self.dismissViewControllerAnimated(true, completion: nil) 

    let allowedChars = 15 
    let passwordCount = passwordField.text?.characters.count 

    if passwordCount <= allowedChars { 
     // allow user to continue if the amount of characters is less than 15 
     alertController.viewDidAppear(false) 

    } else { 
     // allow user to not be able to continue if they have too many characters 

     alertController.viewDidAppear(true) 
    } 
} 
+1

あなたは 'viewWillAppear/disappear'をオーバーライドしている場合を除き、これらのメソッドを直接呼び出してはいけません。 'presentViewController'を呼び出した直後に' dismissViewController'を呼び出すときに、これが明確に定義されているかどうかはわかりません。あなたはUIKitと戦うべきではありません(もしハックのように見えたら、おそらくそれは間違っているでしょう;) – Vinzzz

+1

もう一つ、私はここにキャンセルボタンは必要ないと思います。 –

答えて

4

置き換えますと

self.presentViewController(alertController, animated: true, completion: nil) 
self.dismissViewControllerAnimated(true, completion: nil) 

let allowedChars = 15 
let passwordCount = passwordField.text?.characters.count 

if passwordCount <= allowedChars { 
    // allow user to continue if the amount of characters is less than 15 
    alertController.viewDidAppear(false) 

} else { 
    // allow user to not be able to continue if they have too many characters 

    alertController.viewDidAppear(true) 
} 

let allowedChars = 15 
let passwordCount = passwordField.text?.characters.count 

if passwordCount > allowedChars { 
    self.presentViewController(alertController, animated: true, completion: nil) 
} 
関連する問題