1

I have to squares on the first view controller its hidden at anytime and when the app launches you have two button white and black. when the user presses on either button and press on back to view 1 it shows either the white square and black square depending on which the user picked.

code below:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
    blacksquare.hidden = [SetBlack boolForKey:@"black"];

    NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
    whitesquare.hidden = [SetWhite boolForKey:@"white"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

and

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (IBAction)black:(id)sender{
    NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
    [SetBlack setBool:NO forKey:@"black"];
}

- (IBAction)white:(id)sender{
    NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
    [SetWhite setBool:NO forKey:@"black"];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

2 Answers2

1
- (IBAction)black:(id)sender{
NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
[SetBlack setBool:NO forKey:@"black"];
}

- (IBAction)white:(id)sender{
NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
[SetWhite setBool:NO forKey:@"black"];
}

The above code should be like this

 - (IBAction)black:(id)sender{
NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
[SetBlack setBool:NO forKey:@"black"];
[SetBlack synchronize]; // added synchronize method
}

- (IBAction)white:(id)sender{
NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
[SetWhite setBool:NO forKey:@"black"];
[SetWhite synchronize]; // added synchronize method
}

From the documentation for NSUserDefaults synchronize

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

Bug Hunter Zoro
  • 1,743
  • 18
  • 21
0

You will have to use [[NSUserDefaults standardDefaults] synchronize]; after setting value in NSUserDefaults to save.

Ritu
  • 661
  • 3
  • 8