我正在尝试保存一个简单的功能,用户单击按钮并且图像发生变化,我希望图像在用户单击按钮时保持不变。例如,如果用户单击该按钮并且imageview名为hello
,则当应用关闭并从后台删除时,它会更改为名为goodbye
的图像视图,当用户重新打开应用时goodbye
将显示1}}图像。
我需要做些什么才能保存?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIImageView!
@IBAction func addButton(sender: AnyObject) {
button.image = UIImage(named: "testButtonWin")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
NSUserDefaults.standardUserDefaults().setObject(button, forKey: "button")
NSUserDefaults.standardUserDefaults().synchronize()
//println(NSUserDefaults.standardUserDefaults().objectForKey("button")!)
}
}