Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i have a slider connected to this action. i've declared the static NSString to use for the default's key. and i want to log the results of the slider using the key...

Code:
static NSString *const defaultSizeKEY = @"defaultSizeKEY";


- (IBAction)changeSize:(id)sender
	{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setValue:[NSNumber numberWithInt:[sender intValue]] forKey:defaultSizeKEY];
	NSLog (@"%@", defaultSizeKEY);
	}

unfortunately, this only logs "defaultSizeKEY" when the slider is changed.

Code:
NSLog (@"@i", [sender intValue]);

this works fine, but i need to access the [sender intValue] in a -(void)mouseDragged: (NSEvent *)event method of a different class.

Code:
- (void)mouseDragged:(NSEvent *)event
	{
	NSSize size = NSMakeSize(64, 64);
        }

NSMakeSize(64,64) works fine, but these are the number i need changed using the slider that is bound to the defaultSizeKEY string.

as mentioned above writing NSMakeSize(defaultSizeKEY, defaultSizeKEY) doesn't work, and i can't write NSMakeSize([sender intValue]) because the sender is not connected to this mouseDragged event method...

*head explodes*
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Code:
static NSString *const defaultSizeKEY = @"defaultSizeKEY";


- (IBAction)changeSize:(id)sender
	{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setValue:[NSNumber numberWithInt:[sender intValue]] forKey:defaultSizeKEY];
	NSLog (@"%@", defaultSizeKEY);
	}

The NSLog call will only show the text "defaultSizeKEY", because that is what you told it to show.

Are you sure that you want changeSize to change the application's preferences? (Because that is what NSUserDefaults is: The application preferences. That is most certainly a place that you don't want to use to communicate a value from one method to another).
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
The NSLog call will only show the text "defaultSizeKEY", because that is what you told it to show.

Are you sure that you want changeSize to change the application's preferences? (Because that is what NSUserDefaults is: The application preferences. That is most certainly a place that you don't want to use to communicate a value from one method to another).

seems like only way to hold UI settings, no? i mean, a user's moved position of a slider should be kept, right?
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
seems like only way to hold UI settings, no? i mean, a user's moved position of a slider should be kept, right?

How long do you need to keep this value, though? Just during the running of the app, or across restarts/etc? If the former, then you don't need something like UserDefaults.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Well, you are using NSUserDefaults in the original code correctly.

The problem is that your NSLog is incorrect. You are simply logging the key name, and not the value Otherwise it is fine.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.