How to save the state of a button in iOS development

Source: Internet
Author: User

In iOS development we often need to save the state of a button, such as a lot of settings options in the uiswitch button switch, such as the selection of some buttons or not, and so on, today there is a requirement is to save the automatic login button selected, to achieve this function, you can use the archive to complete.

First demand diagram:

I need to record the status of the automatic login button, if the user chooses to log in automatically the next time, then the program exits again to start this button is still selected.

First, we need to listen for the button's Click event, because we can get the button status in the Click event.

Use the following code to store the selected state.

-(Ibaction) Autologinclick: (UIButton *) Sender {        *defaults = [Nsuserdefaults standarduserdefaults];    [Defaults setBool:sender.selected forkey: @" Autologin " ];    [Defaults synchronize]; // don't forget this line of code }

Then set the control state where you loaded the control, my control is created through Xib, so the button is selected directly inside the viewdidload.

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];     = [Defaults boolforkey:@ "autologin"];

How to save the state of a button in iOS development

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.