【问题标题】:UIAlertController should not be showingUIAlertController 不应该显示
【发布时间】:2016-04-19 02:02:35
【问题描述】:

我正在制作一个带有警报控制器的登录应用程序,如果用户没有正确数量的密码字符,它不应该让用户继续。当我输入正确的金额时,警报控制器会弹出并且不允许我继续。我的代码中有什么我不应该有的吗?

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)
    }
}

【问题讨论】:

  • 除非你重写 viewWillAppear/disappear ,否则你不应该直接调用这些方法。在调用 presentViewController 之后调用 dismissViewController :我不确定这是否具有明确定义的行为。你不应该对抗 UIKit(如果它看起来像一个 hack,你可能做错了;)
  • 我认为这里不需要取消按钮。

标签: ios swift uiviewcontroller


【解决方案1】:

替换:

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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多