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 06-02-2010, 10:37 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Tennessee
Posts: 23
DevinSnipes is on a distinguished road
Default Save UISwitch's Current State

Is it possible to save the state of a UISwitch as soon as the user changes the switches state? I have it where when the user closes the application, the NSUserDefaults synchronize with the application. If anyone has the code, please, please help me out! Thanks and I'll greatly appreciate it!
__________________
-Regards,

Devin Snipes
My Website
Follow Me on Twitter

My AppStore Apps:
Am I Ugly(Free)
uChat For iPhone(Free)
MCS For iPhone(Free)
DevinSnipes is offline   Reply With Quote
Old 06-02-2010, 11:51 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

So you're looking for some sort of event that gets fired when the value of the UISwitch changes? That way you can save the value using NSUserDefaults?
smithdale87 is offline   Reply With Quote
Old 06-02-2010, 08:00 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Tennessee
Posts: 23
DevinSnipes is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
So you're looking for some sort of event that gets fired when the value of the UISwitch changes? That way you can save the value using NSUserDefaults?
Exactly what I'm looking for. Like I said, I have the NSUserDefaults to synchronize when the application terminates.
__________________
-Regards,

Devin Snipes
My Website
Follow Me on Twitter

My AppStore Apps:
Am I Ugly(Free)
uChat For iPhone(Free)
MCS For iPhone(Free)
DevinSnipes is offline   Reply With Quote
Old 06-03-2010, 01:59 AM   #4 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default

Hi Devin!

Well, I haven't worked with UISwitch at all so I don't know exactly how it works, but I think it shouldn't be a problem... You should have a listener to detect when the user changes a UISwitch's state, and a UISwitch have a property called "on" which is a boolean that simply determines if the switch is turned on or not. If the listener exists you simply should save this property's state in a variable so you can store it and save it in your NSUserDefaults (in fact I think you may save the property itself so you don't even need to store it in a variable). What happens if you don't have a listener (as I've said I don't know because I've never worked with UISwitches)? Well, you should then be aware on when the user can change the switch state... Let me explain with some pseudo-code examples:

If the UISwitch have it's own listener:
Code:
-(void)switchHaveChangedItsState:(UISwitch*)switch   // of course if the method exists it will not be called like this ;)
{
     self.myBool = switch.on;
}
What if the UISwitch doesn't have a listener (the following it's just an idea, but sure it can be solved in many ways)?
Code:
// In your touch method:
-(void)touchesBegan(...)
{
     self.switchState = self.mySwitch.on; // save your switch's state...
}

-(void)touchesEnded(...)
{
     if( self.switchState != self.mySwitch.on )
     {
          // If yes means that since the user touched the screen the switch's value have changed, so it means that the user changed it!
          // Here you can save it to NSUserDefaults...
          // To save it you simply should copy your NSUserDefault's code here (I've never used NSUserDefaults, I use to save my data in an own-made plist file)
     }
}
Well I think it is more or less what you're looking for (or what I've understood, but here are 9.00 am so I'm not even awaken ^_^), if you have any other doubt or this isn't exactly what are you looking for ask me and if I can I will help you!

Regards!
Johanovski is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, ipod, objective, touch

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: 351
13 members and 338 guests
ajay123123, Anwerbl, Arty Tales, ashaman64, baja_yu, ChrisYates, HemiMG, mini998, mottdog, newDev, Objective Zero, oceanlablight, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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