IOS project development practice-use user preference data for startup prompts
This case also stores user input based on the user preference data API provided by Apple. For the basic usage of preferences, you can view another blog titled iOS project development practices-using user preferences data API to store information. This instance will prompt whether a dialog box is displayed when the App is started based on the user's selection.
(1) drag a Switch control in the interface and bind it to the code. When you open the Switch, a dialog box will be displayed next time you enter the application. When you close the Switch, no prompt will be displayed next time you enter the application.
(2) implement the following in the Code:
Import UIKitclass ViewController: UIViewController {var userDefault: NSUserDefaults! // User preference statement; @ IBOutlet weak var switchTips: UISwitch! // Bind the Switch control; override func viewDidLoad () {super. viewDidLoad () userDefault = NSUserDefaults. standardUserDefaults () switchTips. on = userDefault. boolForKey (isShowTips) // display the previous switch status; if (userDefault. boolForKey (isShowTips) {// read the switch status from user preferences. If the switch is enabled, a prompt is displayed. UIAlertView (title: tips, message: note that, you have enabled the tip function, delegate: nil, cancelButtonTitle: Quit ). show () }}@ IBAction func switchValueChanged (sender: UISwitch) {// if the status of the Switch control changes, the Bool value is stored in the preference; userDefault. setBool (switchTips. on, forKey: isShowTips)} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}
(3) The program runs as follows:
.