IOS project development practice-use user preference data for startup prompts

Source: Internet
Author: User

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:

 

.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.