0

Bellow I have some code that simply hides a button through a delegation system in Xcode. I believe that this all works however when I transfer views back to the 'leveSelector' view the delegate is not valid so the button does not show as un-hidden. Because of this I would like to apply some NSUserDefaults to save the button state so that when I go back to the'levelComplete' view later in the game I would like the button to be un-hidden.

Here I have the code for the delegate system i am using:

Here I have the levelComplete code... .h

#import <UIKit/UIKit.h>
#import "levelComplete.h"
#import "LevelSelector.h"

@interface levelComplete : UIViewController{
  }
 @property (nonatomic, strong) id<CustomDelegate> delegatePpty;

 @end

.m

@implementation levelComplete
@synthesize delegatePpty;

-(void)someAction
 {
[self.delegatePpty hideUnhidebutton:YES];//Call the delegate method to execute
 }


 - (void)viewDidLoad
   {
     [super viewDidLoad];
     // Do any additional setup after loading the view from its nib.
     [self someAction]; // Here I call my action
   }
@end

Here I have the leveSelector code...

.h

  @protocol CustomDelegate <NSObject>
  -(void)hideUnhidebutton:(BOOL)value;
  @end

#import <UIKit/UIKit.h>
#import "levelComplete.h"
#import "LevelSelector.h"

@interface LevelSelector : UIViewController <CustomDelegate>{        

}

@property (nonatomic, strong) UIButton *level1;

@end

.m

@implementation LevelSelector
@synthesize level1;

-(void)hideUnhidebutton:(BOOL)value
 {
  [self.level1 setHidden:value];

  }

So to clarify I would like to save the button when it has been hidden as a NSUserDefault. In this case the button I desire to hide is called level1.

Edit: thanks to everybody who posted apply your code worked great

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Thomas Stone
  • 287
  • 3
  • 9
  • possible duplicate of [Save string to the NSUserDefaults?](http://stackoverflow.com/questions/3074483/save-string-to-the-nsuserdefaults) – Anoop Vaidya Apr 02 '13 at 11:27

3 Answers3

4

You can save a value to NSUserDefaults in a straightforward manner:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:value]
             forKey:@"SomeSensibleKey"];

You can check the saved state with:

[[defaults objectForKey:@"SomeSensibleKey"] boolValue]
sapi
  • 9,944
  • 8
  • 41
  • 71
  • The first line of the code above loads the standard defaults dictionary for your app. Unless you have special needs (which, from the question, I don't believe you do) that should work fine. – sapi Apr 02 '13 at 12:39
1

Sapi's answer already covers your comment How would I go about loading the NSUserDefaults

BOOL yourBoolValue = YES;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:yourBoolValue] forKey:@"YourVariableKey"];

The last line has already the result you want. The [NSUserDefaults standardUserDefaults] is a singleton and also persistent, so you can create it in every context, having the same instance.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL restoredBoolValue = [[defaults objectForKey:@"YourVariableKey"] boolValue];
[self.level1 setHidden:restoredBoolValue]; 

Ant that's it. You can basically save everything in the NSUserDefaults. Just check out Apple's reference.

Arndt Bieberstein
  • 1,128
  • 1
  • 14
  • 28
1

To save a boolean value to user defaults you have to call setBool method:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"ButtonHiddenKey"];

To load a boolean value from user defaults (in init method):

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
button.hidden = [defaults boolForKey:@"ButtonHiddenKey"];

There are also messages to store other types of variables such as setInteger, setFloat and so on

boweidmann
  • 3,312
  • 2
  • 20
  • 27