I have a main page having 3 textfield in my first view as i navigate to different view and return back to my first view (i.e after i enter all the values i moving to next page).I want my 3 textfield to retain its value when i go back to my main page here my sample code :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *mobile = MobileNum.text;
[defaults setObject:mobile forKey:self.MobileNum.text];
NSString *amountText = amount.text;
[defaults setObject:amountText forKey:self.amount.text];
[defaults synchronize];
NSString *temp = [defaults objectForKey:self.MobileNum.text];
NSString *temp1 = [defaults objectForKey:self.amount.text];
NSLog(#"%# %#",temp,temp1);
at you viewWillDisappear method store the defaults like.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *mobile = MobileNum.text;
[defaults setObject:mobile forKey:#"mobileNumber"];
NSString *amountText = amount.text;
[defaults setObject:amountText forKey:#"amount"];
[defaults synchronize];
again at your viewWillAppear method retrieve the values like.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults valueForKey:#"mobileNumber"] length]>0){
MobileNum.text=[defaults valueForKey:#"mobileNumber"];
}
else {
MobileNum.text=#"";
}
if ([[defaults valueForKey:#"amount"] length]>0) {
amount.text=[defaults valueForKey:#"amount"];
}
else {
amount.text=#"";
}
it will work.
Related
I wanted to know how to save the position of a switch to close and open the application ?
-(IBAction)switch_poids_passagers:(UISwitch *)sender {
NSString *poids_passagers_total;
if(sender.on) {
poids_passagers_total = #"1";
} else {
poids_passagers_total = #"2";
}
[[NSUserDefaults standardUserDefaults] setObject:poids_passagers_total forKey:#"poids_passagers_total"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSUserDefaults can save and retrieve BOOLS with a pair methods it provides:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:self.myUISwitch.on forKey:#"switchValue"];
// ...
Later on, maybe in viewWillAppear...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL switchValue = [defaults boolForKey:#"switchValue"];
self.myUISwitch.on = switchValue;
This question already has answers here:
Attempt to set a non-property-list object as an NSUserDefaults
(12 answers)
Closed 8 years ago.
NSMutableArray *mutableArray = [NSMutableArray array];
Prostate *prostate = [Prostate new];
prostate.prostateCheckTimeString = self.prostateCheckTime.text;
prostate.prostateCubeString =self.prostateCube.text;
prostate.prostateAntigemString =self.prostateAntigem.text;
prostate.peeSpeedMaximumString =self.peeSpeedMaximum.text;
prostate.peeRemenderString =self.peeRemender.text;
[mutableArray addObject:prostate];
NSArray *tempArray = [[NSArray alloc]initWithArray:self.mutableArray];
[Global saveArrayToUserDefault:prostateArray saveValue:tempArray];
NSArray *array = [Global getArrayFromUserDefault:prostateArray];
//Below is my global object
**+(void)saveArrayToUserDefault:(NSString *)userDefaultKey saveValue:(NSArray *)value**
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:value forKey:prostateArray];
[userDefaults synchronize];
}
**+(NSArray *)getArrayFromUserDefault:(NSString *)userDefaultKey**
{
NSArray *temp;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
temp = [userDefaults objectForKey:userDefaultKey];
return temp;
}
-----------------------------------Description ---------------------------------------
I had Been Search for a while , I know NSUserDefault can only store NSArray , I tried to convert NSMutableArray to NSArray , but still got error
-----------------------------------Error ---------------------------------------------
Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType') Attempt to set a non-property-list object (
""
Implement the <NSCoding> protocol in your entity class which you wanna save
-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[defaults setObject:data forKey:keyName];
[defaults synchronize]; }
-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:keyName];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[defaults synchronize];
return myArray; }
Prostate is your custom object and NSUserDefaults does not allow you to save it.
So you have to Archive it before saving to NSUserDefaults and Unarchive it when reading back from NSUserDefaults.
So your methods to save and read user defaults data should look like:
-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[defaults setObject:data forKey:keyName];
[defaults synchronize];
}
-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:keyName];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return myArray;
}
I'm trying to make an appointment form on the iPad and I'm having trouble with the UISwitch, I need to save its state and the later load it on another page, I have done this for a text field and a date picker but for some reason it doesn't work for the UISwitch. I am very new to this so any help would be appreciated.
-(IBAction)savedata:(id)sender; {
NSString *savestring = _NameofResident.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:#"savedstring"];
[defaults synchronize];
NSString *savestring1 = _NameofStudent.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:savestring1 forKey:#"savedstring1"];
[defaults synchronize];
NSString *savestring2 = _AppointmentTime.date;
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
[defaults2 setObject:savestring2 forKey:#"savedstring2"];
[defaults synchronize];
NSString *savestring3 = _NextAppointmentTime.date;
NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
[defaults3 setObject:savestring3 forKey:#"savedstring3"];
[defaults synchronize];
NSString *savestring4 = _Confirmed.text;
NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
[defaults4 setObject:savestring4 forKey:#"savedstring4"];
[defaults synchronize];
}
- (IBAction)loaddata:(id)sender; {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:#"savedstring"];
[_NameofResident setText:loadstring];
NSString *loadstring1 = [defaults objectForKey:#"savedstring1"];
[_NameofStudent setText:loadstring1];
NSString *loadstring2 = [defaults objectForKey:#"savedstring2"];
[_AppointmentTime setDate:loadstring2];
NSString *loadstring3 = [defaults objectForKey:#"savedstring3"];
[_NextAppointmentTime setDate:loadstring3];
NSString *loadstring4 = [defaults objectForKey:#"savedstring4"];
[_Confirmed setText:loadstring4];
}
As you can see I can both load and save the state for the names and times but I don't really know how I can add in a UISwitch to do this.
thanks for your help.
To store the value of a UISwitch state, which is a BOOL, you can do 2 things :
store the BOOL in an NSNumber ([NSNumber numberWithBool:myBool]) and save it with defaults setObject: method. But then, when retrieving the BOOL, you will have to do [defaults objectForKey:#"myKey"].boolValue
store the BOOL itself with defaults setBool:forKey method. And retrieve it with defaults boolForKey method.
Moreover, I've seen you get the defaults many times and synchronize it many times. This is not necessary so I give you an example of what you could have done :
-(IBAction)savedata:(id)sender; {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *savestring = _NameofResident.text;
[defaults setObject:savestring forKey:#"savedstring"];
NSString *savestring1 = _NameofStudent.text;
[defaults setObject:savestring1 forKey:#"savedstring1"];
NSString *savestring2 = _AppointmentTime.date;
[defaults setObject:savestring2 forKey:#"savedstring2"];
NSString *savestring3 = _NextAppointmentTime.date;
[defaults setObject:savestring3 forKey:#"savedstring3"];
NSString *savestring4 = _Confirmed.text;
[defaults setObject:savestring4 forKey:#"savedstring4"];
BOOL savebool5 = _Switch.isOn;
[defaults setBool:savebool5 forKey:#"savedbool5"];
[defaults synchronize];
}
- (IBAction)loaddata:(id)sender; {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:#"savedstring"];
[_NameofResident setText:loadstring];
NSString *loadstring1 = [defaults objectForKey:#"savedstring1"];
[_NameofStudent setText:loadstring1];
NSString *loadstring2 = [defaults objectForKey:#"savedstring2"];
[_AppointmentTime setDate:loadstring2];
NSString *loadstring3 = [defaults objectForKey:#"savedstring3"];
[_NextAppointmentTime setDate:loadstring3];
NSString *loadstring4 = [defaults objectForKey:#"savedstring4"];
[_Confirmed setText:loadstring4];
BOOL loadBool5 = [defaults boolForKey:#"savedbool5"];
[_Switch setOn:loadBool5];
}
You can solve your problem the following way:
//
// ViewController.m
//
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
- (IBAction)switchChanged:(id)sender;
#end
#implementation ViewController
- (IBAction)switchChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:self.mySwitch.on forKey:#"SwitchState"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"SwitchState"];
}
#end
Happy programming !
defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%#",[defaults objectForKey:#"firsttime"])
if([[defaults objectForKey:#"firsttime"]isEqualToString:#"YES"])
{
UISwitch *onoff = (UISwitch *) sender;
if(onoff.on)
{
NSLog(#"yes on1 facebookswitch");
facebookSwitch.on = YES;
[userDefault setValue:#"true" forKey:#"facebooknotify"];
NSLog(#"on");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
[validate alertCommonWithoutMessage:NSLocalizedString(#"val_message",nil) :NSLocalizedString(#"val_facebook_login",nil) :#"OK"];
facebookSwitch.on = YES;
NSLog(#"yes on2 facebookswitch");
}
else {
NSLog(#"val_facebook_conf");
[validate alertCommonWithoutMessage:NSLocalizedString(#"val_message",nil) :NSLocalizedString(#"val_facebook_conf",nil) :#"OK"];
facebookSwitch.on = NO;
[userDefault setValue:#"false" forKey:#"facebooknotify"];
NSLog(#"yes off1 facebookswitch");
I'm using this method,but sumtimes the response is null.
Try this code:
// add this code in your switch touch event
- (IBAction)YourSwitch:(UISwitch*)sender
{
if (UISwitch.on)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"Sound"];
[defaults synchronize];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:false forKey:#"Sound"];
[defaults synchronize];
}
}
// add this code in your viewDidload .
BOOL isSound= [[NSUserDefaults standardUserDefaults] objectForKey:#"Sound"];
if (isSound){
_ref_slider.on=TRUE;
} else {
_ref_slider.on=FALSE;
}
i hope this code is useful for you.
As to why it's not working, the most common problem is [userDefault synchronize]
Also, rather than using string boolean values, why not use:
[userDefault setBool:YES forKey:#"facebooknotify"];
and then access by
BOOL notified = [userDefault boolForKey:#"facebooknotify"];
- (IBAction)Save:(id)sender {
NSString *saveString = taskName.text;
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:saveString forKey:#"savedString"];
[defults synchronize];
//use data that has been given
}
- (IBAction)theTask:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:#"savedString"];
[taskName setText:loadstring];
[super viewDidLoad];
}
Im trying to attach the new label i made on storyboard to attach to theTask method. So when the user types in a task and pushes save the task is placed in a list of other tasks