UserDefaults.standard没有保存swift3 xcode 8

时间:2017-03-20 17:48:51

标签: swift3 xcode8

大家好我正在尝试构建一个简单的一个视图程序来调用和导航语音菜单,我无法让UserDefaults在关闭应用程序之间正确保存。我也有问题要在UITextField中显示文本,我想在启动时保存它,这里是vars的描述:

UserInput - 员工ID的文本字段(需要保存的内容) 切换 - 如果保存它是1保存数据,如果0则不保存 提交 - 提交应保存数据并致电的按钮。

这是我的第一个iPhone应用程序,非常感谢任何帮助!

enter code here

   import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var UserInput: UITextField!



var SaveIt = true
var employID = ""
override func viewDidLoad() {
    super.viewDidLoad()



    // Do any additional setup after loading the view, typically from a nib.
    self.UserInput.delegate = self;
    UserInput.keyboardType = .numberPad
    if let current_eid = UserDefaults.standard.string(forKey: "emp_ID") {
        UserInput.text = current_eid
    }

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



func loadDefaults() {
    let defaults = UserDefaults.standard
    if let UserInput = defaults.string(forKey: "emp_ID") {
        print(UserInput)

    } else {
        // focus on the text field if it's empty
        UserInput.becomeFirstResponder()        }

    UserInput.text = defaults.object(forKey: "emp_ID") as! String?
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
@IBAction func Switch(_ sender: UISwitch) {
    if (sender.isOn == true) {
        SaveIt = true
    }
    else{
        SaveIt = false
    }


}

@IBAction func Submit(_ sender: Any) {
    if (SaveIt) {
        let defaults = UserDefaults.standard
        defaults.set(UserInput.text, forKey: "emp_ID")
        defaults.synchronize()
    }
    let phone = "telprompt://1888247889pppp" + UserInput.text! + "p1p1p1p1";
    let url = URL(string:phone)!
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }           else { UIApplication.shared.openURL(url)
    }

}

}

0 个答案:

没有答案