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