UIAlertController不应该显示

时间:2016-04-18 17:53:15

标签: ios swift uiviewcontroller

我正在制作一个带有警报控制器的登录应用程序,如果用户没有正确的密码字符数,则不应让用户继续。当我输入正确的金额时,警报控制器会弹出,不允许我继续。我的代码中是否有我不应该拥有的东西?

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 个答案:

答案 0 :(得分: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)
}