swift显示默认用户名

时间:2016-05-08 03:22:00

标签: xcode swift

@IBOutlet weak var studyID: UITextField!
@IBOutlet weak var userID: UITextField!

let username: String = self.studyID.text!
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(username, forKey: "username")

下次打开此应用时,如何存储用户名和显示内容?

1 个答案:

答案 0 :(得分:1)

将其放在String

viewDidAppear()

确保成功保存。

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    username = NSUserDefaults.standardUserDefaults().objectForKey("username") as! String
    print(username)
}