2016-04-18 55 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) 
} 
相關問題