Sorry for the constant questions, but in my app, you are able to toggle if 3 UILabels and 1 UIImageview's hidden property is YES or NO via UISwitchs on another page (The settings page). The weird part is, one of the UILabels is hidden, even when it is declared that it is not to be hidden. Here is my code on the settings page.
- (IBAction)changeswitch1 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (artworkswitch.on)
[prefs setInteger:1 forKey:#"AWKey"];
else
[prefs setInteger:0 forKey:#"AWKey"];
}
- (IBAction)changeswitch2 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (titleswitch.on)
[prefs setInteger:1 forKey:#"TKey"];
else
[prefs setInteger:0 forKey:#"TKey"];
}
- (IBAction)changeswitch3 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (artistswitch.on)
[prefs setInteger:1 forKey:#"AKey"];
else
[prefs setInteger:0 forKey:#"AKey"];
}
- (IBAction)changeswitch4 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (volumeswitch.on)
[prefs setInteger:1 forKey:#"VKey"];
else
[prefs setInteger:0 forKey:#"VKey"];
}
All of this is set as the 'value changed' action in IB for the 4 switches.
Here is the code for the main page
if ([prefs integerForKey:#"AWKey"] == 1)
currentArtwork.hidden = NO;
else if ([prefs integerForKey:#"AWKey"] == 0)
currentArtwork.hidden = YES;
if ([prefs integerForKey:#"TKey"] == 1)
currentSong.hidden = NO;
else if ([prefs integerForKey:#"TKey"] == 0)
currentSong.hidden = YES;
if ([prefs integerForKey:#"AKey"] == 1)
currentArtist.hidden = NO;
else if ([prefs integerForKey:#"AKey"] == 0)
currentArtist.hidden = YES;
if ([prefs integerForKey:#"VKey"] == 1)
volumeview.hidden = NO;
else if ([prefs integerForKey:#"VKey"] == 0)
volumeview.hidden = YES;
This is just a guess, since I don't know the res of the code. But maybe it is hidden because another UILabel is positioned infront of it?. You can try:
[self.view bringSubviewToFront:yourLabel];
-Oscar
My solution was to have the label/imageview's hidden property set to NO upon the first launch.
Related
i am trying to save the state of UISwitch .If the UISwitch state is "ON" and when the user quits the app and starts it again ..the app should show the previous state and if the user plans to change the state of UISwitch to "OFF"..he should get a message saying previously he had "ON" state and change to state to "OFF"
if you guys help me out that would be great .Thanks
-(IBAction)notification:(id)sender
{
if (sender == notifyMe)
{
if(notifyMe.isOn == YES)
{
toggle = YES;
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:notifyMe.on forKey:#"switchValueKey"];
[defaults synchronize];
NSLog(#"Notification is ON");
}
else
{
toggle = NO;
NSLog(#"Notification is OFF");
}
}
if ([cellDelegate respondsToSelector:#selector(notificationReqd:)])
{
[cellDelegate notificationReqd:self];
}
}
Change your button action method like:
-(IBAction)notification:(id)sender
{
if (sender == notifyMe)
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
BOOL previousState = [defaults boolForKey:#"switchValueKey"];
if(notifyMe.isOn == YES)
{
toggle = YES;
NSLog(#"Notification is ON");
}
else
{
toggle = NO;
NSLog(#"Notification is OFF");
}
[defaults setBool:toggle forKey:#"switchValueKey"];
[defaults synchronize];
//You can show the message here
NSLog(#"Previous state was %d",previousState);
}
if ([cellDelegate respondsToSelector:#selector(notificationReqd:)])
{
[cellDelegate notificationReqd:self];
}
}
When you want to get the stored data you can use:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
BOOL previousState = [defaults boolForKey:#"switchValueKey"];
How to set/make this into an NSMutableArray?
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:1 forKey:#"KeyI1"];
[prefs setInteger:1 forKey:#"KeyJ1"];
[prefs setInteger:1 forKey:#"KeyI2"];
[prefs setInteger:1 forKey:#"KeyJ2"];
Which I can use for an if statement something like this:
NSInteger I1=[prefs integerForKey:#"KeyI1"];
NSInteger I2=[prefs integerForKey:#"KeyI2"]
NSInteger J1=[prefs integerForKey:#"KeyJ1"]
NSInteger J2=[prefs integerForKey:#"KeyJ2"]
if ( I1 == index1 && K1 == index2){
}
if ( I2 == index1 && K2 == index2){
}
You can't use setInteger: for NSUserDefaults (which uses a NSMutableDictionary structure) directly. You should use setObject:forKey: and objectForKey: and add your integers in NSNumber objects or write a NSMutableDictionary category that will handle setInteger:forKey: and integerForKey:.
I have a UITableView with a UISwitch button in it. I want that when I run my application default value of the UISwitch button should be ON. If I toggle the switch button from on to off the value in the plist should change to OFF. If I quit the app and then I again run my app the value should again default to ON in the plist.
I have tried using NSUserDefaults, it works i.e when I change the value from ON to OFF the value in NSUserDefaults also changes. But when the app is run again the default value is not set to ON.
- (void)viewDidLoad {
[super viewDidLoad];
settings = [[NSMutableArray alloc]initWithObjects:#"Clock",#"Time Format",#"Weather",#"Degrees",nil];
switchControl = [[UISwitch alloc]initWithFrame:CGRectMake(205, 110, 20, 15) ];
[self.switchControl addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:switchControl];
NSString *viewdid = #"ON";
userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:viewdid forKey:#"stateOfSwitch"];
}
- (void)viewWillAppear:(BOOL)animated {
NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:#"stateOfSwitch"];
if([_value compare:#"ON"] == NSOrderedSame){
[switchControl setOn:YES animated:YES];
}
else {
[switchControl setOn:NO animated:YES];
}
[super viewWillAppear:animated];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 4;
}
-(void)switchChanged:(id)sender
{
app= (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *value = #"ON";
userdefaults = [NSUserDefaults standardUserDefaults];
if(!switchControl.on){
value = #"OFF";
[userdefaults setObject:value forKey:#"stateOfSwitch"];
}
[userdefaults setObject:value forKey:#"stateOfSwitch"];
[userdefaults synchronize];
}
//this is my second class where i am accessing my userdefaults key and depending on that hiding the views
- (void) viewWillAppear:(BOOL)animated
{
NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:#"stateOfSwitch"];
if([_value compare:#"ON"] == NSOrderedSame){
newview.hidden = NO;
newnewview.hidden =NO;
firstview.hidden = NO;
secondview.hidden = NO;
thirdview.hidden = NO;
}
else if([_value compare:#"OFF"] == NSOrderedSame) {
newview.hidden = YES;
newnewview.hidden= YES;
firstview.hidden = YES;
secondview.hidden = YES;
thirdview.hidden = YES;
}
}
In appDelegate did finish loading write below statements
[[NSUserDefaults standardUserDefaults] setObject:#"OFF" forKey:#"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
May be you have not dump new values to plist?
Try to call [[NSUserDefaults standardUserDefaults] synchronize]; after setting new value to NSUserDefaults.
Either do
// set when launching the app
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setObject:#"OFF" forKey:#"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
or
// set when terminating
- (void)applicationWillTerminate:(UIApplication *)application
[[NSUserDefaults standardUserDefaults] setObject:#"OFF" forKey:#"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Furthermore, you should test your NSString using isEqualToString: as compare: may lead to false positives (e.g. if one of the strings is nil).
You shouldn't store a string, which has the potential for parsing bugs, when NSUserDefaults is perfectly capable of storing BOOLs directly.
Just store it like this:
#define StateOfSwitch #"StateOfSwitch"
-(void)switchChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:switchControl.on forKey:StateOfSwitch];
[[NSUserDefaults standardUserDefaults] synchronize];
}
and then later, load the value like this:
switchControl.on = [[NSUserDefaults standardUserDefaults] boolForKey:StateOfSwitch];
(Note it's a good idea to never use string keys directly, it's better to #define them to prevent typo errors. Plus you get command + click to jump to definition, autocompletion, etc.)
I want to store a float value into NSUserDefaults.
I also need to check that the float value exists..if not I need to assign some value in it.
and retrieve it...for the above I have the below code but it gives me an error.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"HANDWRITING_SIZE_SLIDER"] == YES) {
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[NSUserDefaults standardUserDefaults] floatForKey:#"HANDWRITING_SIZE_SLIDER"]];
}
Thanks for any help
Use the NSNumber class for this and store it via the setObject:forKey: method so you can check if it exists.
I'd also suggest the usage of constants as keys:
#define HANDWRITING_SIZE_SLIDER #"HSS"
Your code should be along these lines:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:HANDWRITING_SIZE_SLIDER] == nil) {
//doesn't exist in NSUserDefaults, set to default value...
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[defaults objectForKey:HANDWRITING_SIZE_SLIDER] floatValue];
}
Somewhere else in your app, you'd set the value in NSUserDefaults like this:
float sizeSliderValue = ...
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sizeSliderValue] forKey:HANDWRITING_SIZE_SLIDER];
Try this code:
-(IBAction)sliderAction:(id)sender
{
float sliderValue = [sender floatValue];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sliderValue] forKey:#"keySliderValue"];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"keySliderValue"]);
}
I am a new comer to the iPhone world, I am having a question,I am having a login page in which 2 levels and associated 2 uitextfield,i have to save both the UITextfield's Values(inputt by user) to the NSUserDefault,and new time when we start application both the UITextfield should be filled with the values we hav input before....i am in bid trouble
code would be appreciated
Thanx in advance
-(void)saveLoginToUserDefaults:(NSString*)loginString
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:loginString forKey:#"Login"];
}
}
-(NSString*)retrieveLoginFromUserDefaults
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:#"Login"];
return val;
}