import UIKit
class FiveVC: UIViewController {
//MARK:-------- 全局 常量 设置
let IsFirstLaunch = "IsFirstLaunch"
let UserName = "UserName"
let UserPwd = "UserPwd"
var nameField = UITextField()
var PwdField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
nameField = UITextField(frame:CGRect(x:,y:,width:,height:))
nameField.backgroundColor = UIColor.cyan
nameField.placeholder = "请输入姓名"
nameField.borderStyle = UITextBorderStyle.roundedRect
self.view.addSubview(nameField)
PwdField = UITextField(frame:CGRect(x:,y:,width:,height:))
PwdField.backgroundColor = UIColor.cyan
PwdField.placeholder = "请输入密码"
PwdField.borderStyle = UITextBorderStyle.roundedRect
self.view.addSubview(PwdField)
let keetBtn = UIButton.init()
keetBtn.frame = CGRect(x:,y:,width:,height:)
keetBtn.backgroundColor = UIColor.cyan
keetBtn.setTitle("保存", for: UIControlState.normal)
keetBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
keetBtn.addTarget(self, action: #selector(pressKeepBtn), for: UIControlEvents.touchUpInside)
self.view.addSubview(keetBtn)
let reduceBtn = UIButton.init()
reduceBtn.frame = CGRect(x:,y:,width:,height:)
reduceBtn.backgroundColor = UIColor.cyan
reduceBtn.setTitle("清空", for: UIControlState.normal)
reduceBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
reduceBtn.addTarget(self, action: #selector(pressReduceBtn), for: UIControlEvents.touchUpInside)
self.view.addSubview(reduceBtn)
let userName = LCDUserDefaultsGet(key: UserName) as! String
let userPwd = LCDUserDefaultsGet(key: UserPwd) as! String
print("\(userName):\(userPwd)")
if userName.characters.count > {
freshDate(isOk: true, userName: userName, userPwd: userPwd)
}
/*
//obj 为bool值的时候 调用
if LCDUserDefaultsBool(key: UserName) {
let userName = LCDUserDefaultsGet(key: UserName) as! String
let userPwd = LCDUserDefaultsGet(key: UserPwd) as! String
print("\(userName):\(userPwd)")
}
print(LCDUserDefaultsBool(key: UserName))
*/
}
//刷新数据
func freshDate(isOk:Bool, userName:String, userPwd:String){
if isOk {
nameField.placeholder = ""
nameField.text = userName
PwdField.placeholder = ""
PwdField.text = userPwd
}else{
nameField.placeholder = "请输入姓名"
nameField.text = ""
PwdField.placeholder = "请输入密码"
PwdField.text = ""
}
}
//保存数据
func pressKeepBtn(){
if nameField.text?.characters.count == {
alterShow(titles: "请输入姓名")
return
}
if PwdField.text?.characters.count == {
alterShow(titles: "请输入密码")
return
}
LCDUserDefaultsSet(key: UserName,obj: nameField.text as AnyObject)
LCDUserDefaultsSet(key: UserPwd,obj: PwdField.text as AnyObject)
LCDUserDefaultsSyn()
let userName = LCDUserDefaultsGet(key: UserName) as! String
let userPwd = LCDUserDefaultsGet(key: UserPwd) as! String
freshDate(isOk: true, userName: userName, userPwd: userPwd)
alterShow(titles: "数据已保存成功")
}
//清空数据
func pressReduceBtn(){
LCDUserDefaultsSet(key: UserName,obj: "" as AnyObject)
LCDUserDefaultsSet(key: UserPwd,obj: "" as AnyObject)
LCDUserDefaultsSyn()
freshDate(isOk: false, userName: "", userPwd: "")
alterShow(titles: "数据已清空成功")
}
//MARK:------------- LCDUserDefaults
//保存
func LCDUserDefaultsSet(key:String, obj:AnyObject) -> Void {
return UserDefaults.standard.set(obj, forKey: key)
}
//获取
func LCDUserDefaultsGet(key:String) -> AnyObject {
return UserDefaults.standard.value(forKey: key)! as AnyObject
}
//删除
func LCDUserDefaultsRemo(key:String) -> Void {
UserDefaults.standard.removeObject(forKey: key)
}
//判断bool 类型的值
func LCDUserDefaultsBool(key:String) -> Bool {
return UserDefaults.standard.bool(forKey: key)
}
func LCDUserDefaultsSyn() {
UserDefaults.standard.synchronize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func alterShow(titles:String){
let alertController = UIAlertController(title: "提示",
message: titles, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default,
handler: {
action in
print("点击了确定")
})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}