Xcode: Check if UIButton is hidden, using NSUserDefaults - xcode

How would I allow the app to save & load the current state of a button (i.e. if it is hidden, how would I save this, so that next time it will be hidden)? Also, I have it currently set so that when a button is pressed, all the other buttons become un-hidden. How can I set it so that certain ones don't become un-hidden if they have been set un-hidden in NSUserDefaults if you see what I mean?
Thanks

You can check with Apple's documentation on NSUserDefaults to get any additional information you need.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:myBool boolForKey:#"hiddenButton"];
[defaults synchronize];
Then you can just pull it in the same way and set your hidden value.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
myButton.hidden = [defaults boolForKey:#"hiddenButton"];
EDIT:
This would be one way to implement, but it really depends on what you want.
myViewController.h
BOOL myButtonState;
myViewController.m
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
myButton.hidden = [defaults boolForKey:#"hiddenButton"];
}
-(IBAction)buttonPressed {
myButtonState = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:myButtonState boolForKey:#"hiddenButton"];
[defaults synchronize];
}

Related

how to save login data using NSUserDefaults and checkbox in Xcode?

hi i am trying to store my username and password in my app with NSUserdefaults and checkbox.
my code is ..
In viewdidload
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:UserText.text forKey:#"infoString"];
[defaults setObject:PasswdText.text forKey:#"infoString1"];
[defaults synchronize];
UserText.text=[[NSUserDefaults standardUserDefaults] objectForKey:#"infoString"];
and
- (IBAction)checkButton:(id)sender {
if (!checked) {
[checkBoxButton setImage:[UIImage imageNamed:#"checkbox_ticked.png"] forState:UIControlStateNormal];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.UserText.text=[defaults objectForKey:#"infoString"];
self.PasswdText.text = [defaults objectForKey:#"infoString1"];
checked = YES;
}
else if (checked) {
[checkBoxButton setImage:[UIImage imageNamed:#"checkbox_not_ticked.png"] forState:UIControlStateNormal];
checked = NO;
}
}
and my result is if i click checkbox means my textfield is refreshed.where i made mistake?
thanks in advance.
You should save the values in NSUserDefaults when User Performed Submit action, Like this
- (void)submitButtontapped {
if (checked) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:UserText.text forKey:#"infoString"];
[defaults setObject:PasswdText.text forKey:#"infoString1"];
[defaults synchronize];
UserText.text=[[NSUserDefaults standardUserDefaults] objectForKey:#"infoString"];
}
// do the next thing
}
- (IBAction)checkButton:(id)sender {
if (checked) {
checked = NO;
[checkBoxButton setImage:[UIImage imageNamed:#"checkbox_not_ticked.png"] forState:UIControlStateNormal];
} else {
checked = YES;
[checkBoxButton setImage:[UIImage imageNamed:#"checkbox_ticked.png"] forState:UIControlStateNormal];
}
}

How do I programmatically change system-wide desktop picture?

I'm using the following code to set desktop picture:
NSURL* newImage = [[NSURL alloc] initFileURLWithPath:#"/Users/name/Pictures/test.png"];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:newImage forScreen:screen options:nil error:&nserror];
It works fine and the desktop picture changed as I required. But it does not change the system-wide desktop preferences (for example, change picture every 30 minutes). How can I change the system-wide desktop preferences?
Take a look at Preferences and Settings Programming Guide. This should help.
Edit:
Here is the sample code:
NSString* newImgPath = #"/Users/cody/Desktop/stuff/imgs/Black_mac.png";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary* desktopDict = [NSMutableDictionary dictionaryWithDictionary:[defaults persistentDomainForName:#"com.apple.desktop"]];
NSMutableDictionary* bgDict = [desktopDict objectForKey:#"Background"];
NSMutableDictionary* spaces = [bgDict objectForKey:#"spaces"];
[spaces enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSMutableDictionary* obj, BOOL *stop) {
[obj enumerateKeysAndObjectsUsingBlock:^(id key, NSMutableDictionary* prefs, BOOL *stop) {
[prefs setObject:newImgPath forKey:#"ImageFilePath"];
[prefs setObject:newImgPath forKey:#"NewImageFilePath"];
[prefs setObject:#"Never" forKey:#"Change"];
}];
}];
//NSLog(#"%#", desktopDict);
[defaults setPersistentDomain:desktopDict forName:#"com.apple.desktop"];
if ([defaults synchronize] == NO)
NSLog(#"synchronize failed");
// Restart dock
system ("/usr/bin/killall Dock");

Cocoa - Store an NSString value

I want to store an NSString variable, that I receive from a JSON request, for future use. So when the user loads the app again, it loads that value (an NSString) that I stored.
What is the best way to store that kind of information?
NSUserDefaults is what you're looking for.
Save string:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:theStringToSave forKey:#"keyToLookupString"];
[prefs synchronize];
Retrieve string:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *theSavedString = [prefs stringForKey:#"keyToLookupString"];

Change a loaded image based on a NSUserDefaults setting

Hope you can help me with this.
I have an app that needs to show one of two maps via the setting of a UISwitch. The settings.bundle is all set up and I am trying to write an If statement to determine if the switch is on or off, and to display the right image.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL Enabled = [defaults boolForKey:#"zones_preference"];
if (Enabled == #"Enabled") {
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withzones.jpg"]];
}
else {
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withoutzones.jpg"]];
}
This builds without error, but doesn't load the image into the ScrollView. Could anyone advise on where I am going wrong?
Well, the code you've posted only creates a UIImageView object and doesn't do anything more. It's a leak too.
There's one more error in the line
if (Enabled == #"Enabled") {
Here, you are comparing a boolean to a string which will evaluate to false automatically so it needs to be corrected too.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"zones_preference"];
UIImageView * imageView;
if ( enabled ) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withzones.jpg"]];
} else {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withoutzones.jpg"]];
}
imageView.frame = imageViewFrame; // Where "imageViewFrame" is an appropriate frame.
[scrollView addSubview:imageView];
[imageView release];
Deepak, that was very useful and thank you. I had been working with a variable, but the mistake I had made was that I was trying to add the setup of the variable in with the creation of the UIImageView.
I'll work with this and see how I get on.

Saving title of NSSegmentedControl in User defaults

Thanks for the help:
I manually set the title of a segController segment from a textField input like this:
NSString *labelString = [textField stringValue];
(textField.stringValue = labelString);
[segControl setLabel: labelString forSegment:8];
I loose the new label when quitting. How can I save the edited segController label string in NSUserDefaults as I would with a text string, like this:
[[NSUserDefaults standardUserDefaults] setObject: [textField objectValue] forKey: #"newDefault"];
My action needs to occasionally set a new title. Point is the label string is not permanently fixed.
thanks.
Paul.
Assuming you know the segment number, You can do the following:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if(defaults) {
[defaults setValue: [segControl labelForSegment:8] forKey: #"segmentLabel"];
}
else {
// handle error
}
Alternatively you can just save the string to NSUserDefaults whenever you set the label like in your above example.

Resources