使用IBAction按钮对用户登录尝试进行计数

时间:2019-06-12 11:28:04

标签: swift ibaction

我想计算单击登录按钮的次数。如果用户名和密码字段为空并且单击登录按钮,我希望计数增加1。

由于我不能在IBAction函数中将“ count”作为inout参数,我该如何完成此任务?

@IBAction func loginButtonAction(_ sender: Any) {

    var count : Int = 0
    if (Username.text!.isEmpty || Password.text!.isEmpty ) {
        let myAlert = UIAlertController(title: "Alert", message: "All fields are required to fill in", preferredStyle: UIAlertController.Style.alert)
        let OkAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
        myAlert.addAction(OkAction)
        self.present(myAlert,animated: true, completion: nil)
        return
    }

1 个答案:

答案 0 :(得分:2)

您可以使用静态变量:

class MyLogs {
    static var numberOfLoginAttempts = 0

    init() {}

    func loginAttempt() {
        numberOfLoginAttempts += 1
    }

}

用法:

let logs = MyLogs()

@IBAction func loginButtonAction(_ sender: Any) {

    logs.loginAttempt()
    print("Login Attempt Number : \(MyLogs.numberOfLoginAttempts)")
    var count : Int = 0
    if (Username.text!.isEmpty || Password.text!.isEmpty ) {
        let myAlert = UIAlertController(title: "Alert", message: "All fields are required to fill in", preferredStyle: UIAlertController.Style.alert)
        let OkAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
        myAlert.addAction(OkAction)
        self.present(myAlert,animated: true, completion: nil)
        return
    }
}

如果您要关闭应用程序但仍然有登录次数:

看看CoreData