Saving title of NSSegmentedControl in User defaults - cocoa

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.

Related

Saving NSTextField properties based on NSPopUpButton selection using NSUserDefaults

I have added an option to my app to change the background color and textColor of an NSTextField. I set up an NSPopUpButton and based on the selected item in the NSPopUpButton, it changes the colors. I want to save this selection using NSUserDefaults. I am using this method to change the backgroundColor and textColor and it works. How would I save the properties with NSUserDefaults and have it set on start up?
- (IBAction)addBarColor:(id)sender {
if ([addBarColor.titleOfSelectedItem isEqualToString:#"White"]) {
addressBar.backgroundColor = [NSColor whiteColor];
addressBar.textColor = [NSColor blackColor];
}
else {
//default state
addressBar.backgroundColor = [NSColor redColor];
addressBar.textColor = [NSColor whiteColor];
}
}
First set in your action method like that below:-
Now in this action whenever you set the color, it will save into default color just added here two lines
- (IBAction)addBarColor:(id)sender {
NSUserDefaults *default=[NSUserDefaults standardUserDefaults];
[default setObject:addBarColor.titleOfSelectedItem forKey:#"selectedColor"];
//Process your code
}
//Now in this just reading the saved color from defaults and then setting into your popup button
-(void)awakeFromNib
{
NSUserDefaults *default=[NSUserDefaults standardUserDefaults];
NSString *defColor=[default ObjectForKey:#"selectedColor"];
if (defColor)
{
[addBarColor selectItemWithTitle:defColor];
}
}

Can not update NSUserDefaults NSMutableDictionary Object

I'm trying to Update a Dictionary, saved in the UserDefaults.
However, it just doesn't want to update the Object at the given Index. I have no idea why. The Object with it's values stay the same.
Code:
- (void) saveUser
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *users = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:#"users"]];
BOOL userAlreadyAdded = FALSE;
NSUInteger count = 0;
for (NSMutableDictionary *user in users) {
if ([[user objectForKey:#"username"] isEqualToString:[defaults objectForKey:#"username"]]) {
NSLog(#"User found: %#", user);
[user setObject:sid forKey:#"sess"];
[user setObject:mid forKey:#"seca"];
[users replaceObjectAtIndex:count withObject:user];
userAlreadyAdded = TRUE;
}
count++;
}
if (!userAlreadyAdded) {
NSMutableDictionary *userToAdd = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[defaults objectForKey:#"username"], #"username",
sid, #"sess",
mid, #"seca",
nil];
[users addObject:userToAdd];
}
NSLog(#"USERS TO SAVE: %#", users);
[defaults setObject:users forKey:#"users"];
[defaults synchronize];
}
Values returned from NSUserDefaults are immutable, even if you set a
mutable object as the value. For example, if you set a mutable string
as the value for "MyStringDefault", the string you later retrieve
using stringForKey: will be immutable.
try removing the object and adding it again.
For each user create a Mutable Dictionary from the one you have like you already did with 'users' (initWithArray).
NSMutableDictionary* mutableUser = [NSMutableDictionary dictionaryWithDictionary:user];
Also I'm not entirely sure you're safe to presume that the object at index of your count is the object you expect because the for in loop enumerates differently. I would use setObjectForKey, not replaceAtIndex.

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");

Xcode: Check if UIButton is hidden, using NSUserDefaults

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];
}

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.

Resources