我想计算一下登录按钮被点击了多少次。如果用户名和密码字段为空,并且单击了登录按钮,我希望计数递增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
}
发布于 2019-06-12 19:54:55
你可以使用静态变量:
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
https://stackoverflow.com/questions/56561126
复制相似问题