How can I save the state of a button to NSUserDefaults? - ios

I need help trying to do a crude type of confirmation where you need to type: I got it! into a NSTextField then press button1 so that once they do press button1 I make my button2 enabled using
-(IBAction)check:(id)sender{
NSString *string = [NSString stringWithValue:#"I Got It!"];
if(field.stringValue isEqualToString:string){
[field setHidden:YES];
[button1 setHidden:YES];
[button2 setEnabled:YES]
}
}
This is only a one-time confirmation so I'm wondering how I can save the state of the button so that next time they launch the app they don't have to do the confirmation again. The textfield and button1 will be hidden and so that button2 will always be enabled, I want to use the NSUserDefaults because I think that would be the easiest for me to understand.

You can use as below
if ([[NSUserDefaults standardUserDefaults]
boolForKey:#"ishidden"] != YES){
// First launch
} else { //not first launch }

Have a look at NSUserDefaults Class Reference. You can use - (void)setBool:(BOOL)value forKey:(NSString *)defaultName

Related

Have one button go to a different view and enable button

I am working on an app where I have multiple buttons on one page that you click on to go to a different view. The thing is I would like the user to click on them in a specific order. So basically, have all buttons locked except for one and then each time you click on a button that is enabled, it unlocks a new one.
Thanks.
You can do this easily via Tag
When you creating view at that time just put FirstButton in Enabled mode.
OnClick event on every UIButton you can Enabled next one.
For example
i created View with 4 UIButton and given tag 1,2,3,4 at ViewLoad only UIButton with Tag 1 is only Enabled
For all 4 UIButton i have created common Action method as follow which Enabled next UIButton with next Tag
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if([[NSUserDefaults standardUserDefaults] valueForKey:#"LastEnableButton"]){
UIButton * btnTemp = (UIButton *) [self.view viewWithTag:[[[NSUserDefaults standardUserDefaults]valueForKey:#"LastEnableButton"] intValue]];
[btnTemp setEnabled:YES];
NSLog(#"If you want to enable all button which are enabled previously then make for loop up to NSUserDefaults value from starting value");
}
}
- (IBAction)btn_click : (id)sender {
UIButton * btnTemp = (UIButton *) [self.view viewWithTag:([sender tag] + 1)];
if(btnTemp) {
if([btnTemp isKindOfClass:[UIButton class]]) {
[btnTemp setEnabled:YES];
[[NSUserDefaults standardUserDefaults]setValue:[NSNumber numberWithInt:([sender tag] + 1)] forKey:#"LastEnableButton"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
}

How to save a checkbox value when click a save button in ios

I have a check box designed through a button and i am able to change the checkbox images but i am unable to save that last image that is checked in the checkbox when i click on Save button.
I have a page in which a check box is there and save button action is also there when i click on save the value in the checkbox must be saved and when i again re-enter in that screen the previous whatever the value in the checkbox must be seen, but for me every time when i enter into this screen the checkbox is seen empty.
Hope someone helps me out.
BOOL checked;
- (IBAction)canFollowAction:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:#"checkBox.png"]])
{
[sender setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
else
{
[sender setImage:[UIImage imageNamed:#"checkBox.png"]forState:UIControlStateNormal];
}
}
- (IBAction)btnSaveAction:(id)sender
{
if(checked)
{
[btnCheckCanFollow setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
else
{
[btnCheckCanFollow setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
}
[self.navigationController popViewControllerAnimated:YES];
[[[iToast makeText:#"Successfully Updated"]setGravity:iToastGravityBottom]show];
}
#kool kims - The term you used Save value. There any many ways to keep persistent data of your application.
Use NSUserDefaults to save values at application level.
Use .plist file
CoreData
SQlite
Document directory
As per your application architecture you can choose from above, which is more relevant and efficient for your application.
EDIT:
The simplest way if there are not more than one checkbox, You can use NSUserDefaults
Save check value when you click on Save button
- (IBAction)btnSaveAction:(id)sender
{
NSNumber *checkValue = [NSNumber numberWithBool:checked];
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setObject:checked forKey:#"kCheckBoxValue"];
[standardDefaults synchronize];
if(checked)
{
[btnCheckCanFollow setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
else
{
[btnCheckCanFollow setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
}
[self.navigationController popViewControllerAnimated:YES];
[[[iToast makeText:#"Successfully Updated"]setGravity:iToastGravityBottom]show];
}
Whenever you open that ViewController you get previous values from NSUserDefaults from -viewDidLoad or -viewWillAppear.
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if ([standardDefaults objectForKey:#"kCheckBoxValue"]) {
NSNumber *checkValue = [standardDefaults objectForKey:#"kCheckBoxValue"];
checked = [checkValue boolValue];
} else {
checked = NO;
}
}
You Must Save the state in NSUSERDEFAULTS!
For Example ON Button Click
if (button.selected)
{
[button setSelected:YES];
[[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:#"REMEMBER_ME"];
}
and check on ViewDidLoad
if([[NSUserDefaults standardUserDefaults] boolForKey:#"REMEMBER_ME"] == YES)
{
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
}
else
{
[button setSelected:NO];
[button setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
}
Take a global variable to strore the status of check box or in NSUserdefaults u can save the status of button. & chek this status on viewdidload method of controller.
According to what I understood Lets take an example
#property (weak, nonatomic) IBOutlet UIButton *yourButton;
and its Implementation Action
- (IBAction)yourMethodName:(UIButton *)sender {
int tag=_yourButton.tag; //if you have tagged your UI button save it to know which button has changed its state.
if(sender.isSelected){ //Checking state of the button whether selected or not.
UIImage *buttonImage=sender.currentImage;//save this image if your selected images randomly.
NSString *buttonText=sender.currentTitle;//gets you current text in button.
}else{
//selected state is NO
//checkBox is Off.
}
}
here I have showed how you can get different values you need only to store your state as I believe your working with one button/checkbox and you can populate your image based on that state.
However if you have multiple checkboxes or UIbutton you may want to Tag each UIButton your using
[_yourButton setTag:1];
and store tag value too inside action method using sender.tag
EDIT:
Just now went through your code
BOOL checked; value is never set or stored.
so everytime if condition fails.
Also If you have multiple UIButtons you may want to store checked state and tag for the UIButton as I have mentioned earlier;

How to keep UITextField text when the view changes

I've got two UITextFields, the input of which I store into strings player1 and player2. These UITextFields are on a ViewController called by a popOver segue. How can I make the UITextFields keep displaying their text once the view has changed?
I tried textFieldOne.text = player1; in the viewDidLoad section of the ViewController to no avail. Any ideas?
If your loaded view's delegate isn't ViewController, your code wouldn't be executed. So be sure that your code is on the delegate of the loaded view. Use also [textFieldOne setText:player1]. It's always better to call the setter method instead of setting the ivar directly. Then be sure that your UITextField is not nil and correctly binded. Use textFieldOne = [[UITextField alloc] init] to initialise it. If your problem continues, try also [textFieldOne setText:self.player1]. Hope it helps..
EDIT :
Got the solution here. You should use NSUserDefaults so your player names are stored and can be used in each view and even after re-opening your app (if you don't want this you can erase the defaults at lunch. Here is your bunch of code you need to change :
hardOne.m :
- (void)viewDidLoad
{
[super viewDidLoad];
[hard1ON setOn:switchState animated:NO];
//read player names to user defaults
[textFieldOne setText:[[NSUserDefaults standardUserDefaults] stringForKey:#"player1"]];
[textFieldTwo setText:[[NSUserDefaults standardUserDefaults] stringForKey:#"player2"]];
}
- (IBAction) returnKey1
{
player1 = [textFieldOne text];
[players addObject:(player1)];
//set player1's name to user defaults
[[NSUserDefaults standardUserDefaults] setValue:[textFieldOne text] forKey:#"player1"];
}
- (IBAction) returnKey2
{
player2 = [textFieldTwo text];
[players addObject:(player2)];
NSLog(#"array: %#",players);
//set player2's name to user defaults
[[NSUserDefaults standardUserDefaults] setValue:[textFieldTwo text] forKey:#"player2"];
}

How to make a START/STOP button for an iOS app

just wondering how to make a button "START" that when pressed triggers a function and the text changes to "STOP". then when pressed again the function will stop and the text changes back to "START"
Ive got the button already that starts the function. and i can handle changing the title, just not sure on what to use to make the 1 button have 2 functions
Add the IBAction method like:
- (IBAction)buttonTapped:(id)sender
{
UIButton *btn = (UIbutton *)sender;
NSString *title=btn.titleLabel.text;
if ([title isEqualToString:#"Start"])
{
//Start
}else
{
//Stop
}
}
Please try this:
- (IBAction) buttonAction:(id)sender
{
if([[(UIButton *)sender currentTitle]isEqualToString:#"START"])
{
[actionButton setTitle:#"STOP" forState:UIControlStateNormal];
//start the action here and change the button text to STOP
}
else if([[(UIButton *)sender currentTitle]isEqualToString:#"STOP"])
{
[actionButton setTitle:#"START" forState:UIControlStateNormal];
//stop the action here and change the button text to START
}
}
you have at least two options here:
Check for the title of your button and depending on the value call an action or the other.
Create two different buttons, each one with his action and show/hide them.

Flaw in my logic? iOS button states

I have a floor in my logic, I'm struggling to find it. I have three buttons on my app which have two states, on and off. I have different images for each state, the buttons and image swapping is working well. My problem is on load, no matter what state the buttons are saved to, my buttons load to a selected state.
In my viewDidLoad I grab the states from memory:
NSString *_greyButtonSavedState = [[NSUserDefaults standardUserDefaults] stringForKey:#"greyButton"];
I then immediately check the state and apply the correct image (this isn't working):
if ([_greyButtonSavedState isEqualToString:#"ON"]) { [_greyButton setSelected:YES]; } else { [_greyButton setSelected:NO]; }
Each time the button is pressed I run the following:
- (IBAction) _greyButtonPress:(id)sender {
if ([sender isSelected]) {
NSLog(#"Grey map not created");
[sender setImage:_unselectedGrey forState:UIControlStateNormal];
[sender setSelected:NO];
//save state to memory
[[NSUserDefaults standardUserDefaults] setValue:#"OFF" forKey:#"greyButton"];
}else {
NSLog(#"Grey map created");
[sender setImage:_selectedGrey forState:UIControlStateSelected];
[sender setSelected:YES];
//save state to memory
[[NSUserDefaults standardUserDefaults] setValue:#"ON" forKey:#"greyButton"];
}
}
The logs show that on viewDidLoad the image being used is 'selected' but the button is in an 'unselected' mode. I have tried with and without:
[[NSUserDefaults standardUserDefaults] synchronize];
being called each time the button is pressed, no difference.
Any help would be great, thanks.
Well, I notice in one case you use UIControlStateNormal, but the other you use UIControlStateSelected. I think that is the source of your problem.
You are saying "If it is selected, set the image to unselected" and "If it is not selected, set the selected image to selected.

Resources