gpt4 book ai didi

swift - 带输入密码的警报 View

转载 作者:搜寻专家 更新时间:2023-10-31 22:11:29 25 4
gpt4 key购买 nike

在下面的代码中,实现成功。只需要一个修改。应用程序最初打开时,管理员密码应采用默认密码“Hello”,然后当用户有权访问管理员密码时,将对其进行编辑并保存,然后将其保存在 key “管理员密码”中

我怎样才能使默认密码成为可能?

func enableSectionAlert() {
let alertController = UIAlertController(title: "Admin Password", message: "Please input admin password", preferredStyle: .alert)

let enable = UIAlertAction(title: "Enable", style: .default) { (_) in
let field = alertController.textFields?[0].text
if let x = UserDefaults.standard.string(forKey: "admin password"), x == field {

self.demosSelectionEnabled()
self.showSection.isUserInteractionEnabled = false
self.showSection.textLabel?.textColor = UIColor.lightGray
}
else{

let wrongPwd = UIAlertController(title: "Wrong Admin Password", message: nil, preferredStyle:UIAlertControllerStyle.alert)
wrongPwd.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(wrongPwd, animated: true, completion: nil)


}
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

alertController.addTextField { (textField) in
textField.placeholder = "Admin Password"
textField.isSecureTextEntry = true
}

alertController.addAction(enable)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
}

最佳答案

我相信您希望默认密码为“Hello”,并且可以选择稍后更改它,并保留在 userDefaults 中。如果是这种情况,为什么不最初设置默认值如下:

                UserDefaults.standard.set("Hello", forKey: "admin password")

可能在 AppDelegate 中只设置一次,使用以下方法:

    private func setDefaultPassword() {
if UserDefaults.standard.string(forKey: "admin password") == nil {
UserDefaults.standard.set("Hello", forKey: "admin password")
}
}

关于swift - 带输入密码的警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43852509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com