Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-15-2011, 12:59 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 44
tomDev is on a distinguished road
Default Saving "setAlpha" value this NSUserDefaults

Hi,

I might sound like a noob but I'm having trouble to save the Alpha value of a button. When I click on this button it dissapears with an animation, with this code:

Code:
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.0];
	[button1 setAlpha:0.0];
	[UIView commitAnimations];
I use the following code to save a textfield, but and I cant figure how to save the setAlpha value of "button1". How can I adapt the code to save this alpha changes? Is it possible?

Code:
- (IBAction) buttonSave {
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:field.text forKey:@"savedText"];
	[defaults synchronize];
}
I really appreciate if someone can help me.

EDIT: Sorry about the error on the title. "This" = "With" lol.
tomDev is offline   Reply With Quote
Old 02-15-2011, 01:07 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 44
tomDev is on a distinguished road
Default

Im using this code to save alpha and looks like it is working:

Code:
- (IBAction) buttonSave {
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	[defaults setFloat:button1.alpha forKey:@"savedAlpha"];
	[defaults synchronize];
}
But Im getting an error when I try to load the data with this code:

Code:
- (void)viewDidLoad {
	button1.alpha = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedAlpha"];
    [super viewDidLoad];
}
The error is "Incompatible type of argument of setAlpha."
tomDev is offline   Reply With Quote
Old 02-15-2011, 01:11 PM   #3 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

Just read the documentation more carefully. If you are using setFloat, you need to use floatForKey.
JasonR is offline   Reply With Quote
Old 02-15-2011, 01:20 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 44
tomDev is on a distinguished road
Default

JasonR

Dude, you're awesome

At least I didnt ask for the code, I try to do by myself, and I almost did it right hehehe.
tomDev is offline   Reply With Quote
Old 02-15-2011, 01:25 PM   #5 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

Glad to hear you got it working.
JasonR is offline   Reply With Quote
Old 02-16-2011, 02:41 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 44
tomDev is on a distinguished road
Default

I'm having a small problem.

When the app is opened for the first time on a new device, the "viewDidLoad" loads data from a file, but this file is still blank (no data has been written on it yet), so it is setting all values to 0!

It doesnt matter if on the interface builder I set the alpha to 1, when the app is opened on a new device it will always set the alpha to 0... Any ideas of how can I fix this?

Last edited by tomDev; 02-16-2011 at 02:48 AM.
tomDev is offline   Reply With Quote
Old 02-16-2011, 12:29 PM   #7 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

The easiest solution is if the float you get from NSUserDefaults is 0.0, just set the alpha to 1.0 instead. If you really need to store an alpha of 0.0, you will have to add another variable, probably a BOOL, to keep track of whether it's the first time running the app, and set it to 1.0 when running the first time.
JasonR is offline   Reply With Quote
Old 02-16-2011, 05:16 PM   #8 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 44
tomDev is on a distinguished road
Default

Quote:
Originally Posted by JasonR View Post
The easiest solution is if the float you get from NSUserDefaults is 0.0, just set the alpha to 1.0 instead. If you really need to store an alpha of 0.0, you will have to add another variable, probably a BOOL, to keep track of whether it's the first time running the app, and set it to 1.0 when running the first time.
Thanks, I appreciate your help. Im working on a small puzzle game where a few buttons and labels will disappear and change location, and I must save some data about their current alpha and location, and everything is working so far.
tomDev is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 373
9 members and 364 guests
chemistry, cpsclicker, daudrizek, jeroenkeij, Kirkout, PavelMik, teebee74, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:05 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0