Reputation: 287
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
Upvotes: 0
Views: 719
Reputation: 3342
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
Upvotes: 1
Reputation: 1128
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.
Upvotes: 1
Reputation: 10224
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]
Upvotes: 4