0

I gave it all but I just can't figure this one out... need your help please.
What I am doing:
I have an app with a navbar and a tableview as the main screen. Up top in the navbar I have placed a UISwitch (which changes the content of the tableview) and a settings button. The default state of the switch is off and when the user flips it, I check if all of his account info in the settings is filled out and based on that I either allow him to switch on or give him an alert (that he should fill out his account info first) and keep the switch off.
On the accountView in the seetings I have a "Save" button that if pressed, uses NSUserDefaults to store the info and also one string that saves as "true" if all account info is filled in (saved as FNAccountFiledKey). My problem:
Everytime I launch the app, the uiSwitch is for some strange reason evaluating the FNAccountFiledKey as if "true" even tho NSLog right before the if statement says it is set to "false". - the interesting thing is that if I go to the accountView, press the "Save" button and go back, the uiSwitch starts working as desired.

Here is my code:

the switchChanged method

{
NSString *accountInfo=[[NSUserDefaults standardUserDefaults]objectForKey:@"FNAccountFiledKey"];

NSLog(@"%@",accountInfo);

if (accountInfo==@"false") {

    [self.switch1 setOn:NO animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please fill out ALL your Account info first!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];

} else { //if all account info is filled out proceed as usual

if (self.switch1.on) {
    self.tableView.rowHeight= 76;
} else {
    self.tableView.rowHeight= 590;
}

//reload data when switch is changed
    [[self tableView]reloadData];}

and the code for the save button in the AccountView:

NSString *name=[nameField text];
NSString *surname=[surnameField text];
NSString *gender=[genderField text];
NSString *birth=[birthField text];
NSString *email=[emailField text];
NSString *phone=[phoneField text];

[[NSUserDefaults standardUserDefaults]setObject:name forKey:FNAccountNameKey];
[[NSUserDefaults standardUserDefaults]setObject:surname forKey:FNAccountSurnameKey];
[[NSUserDefaults standardUserDefaults]setObject:gender forKey:FNAccountGenderKey];
[[NSUserDefaults standardUserDefaults]setObject:birth forKey:FNAccountBirthKey];
[[NSUserDefaults standardUserDefaults]setObject:email forKey:FNAccountEmailKey];
[[NSUserDefaults standardUserDefaults]setObject:phone forKey:FNAccountPhoneKey];

NSString *tru=@"true";
NSString *fals=@"false";

if ((name.length>0)&&(surname.length>0)&&(gender.length>0)&&(birth.length>0)&&(email.length>0)&&(phone.length>0)) {
    [[NSUserDefaults standardUserDefaults]setObject:tru forKey:FNAccountFiledKey];
} else { [[NSUserDefaults standardUserDefaults]setObject:fals forKey:FNAccountFiledKey];
        }
NSLog(@"Account info complete: %@", [[NSUserDefaults standardUserDefaults]objectForKey:FNAccountFiledKey]);

Does anyone have an Idea what could cause this behavior?
Thanks

4

2 回答 2

3

try like this for comaparing string don't use '==' operator use like this isEqualToString .

if ([accountInfo isEqualToString:@"false"]) {

}
于 2013-04-19T07:28:06.773 回答
0

First thing that you should try is to replace the Logical Operator "==" with "isEqualToString:" like this

  if([accountInfo isEqualToString:[NSString stringWithFormat:@"false"]])
  {
       //Do your stuff here
  }

also soon after you make any changes in the NSUserDefault , you must also synchronize it

  [[NSUserDefaults standardUserDefaults] synchronize];
于 2013-04-19T07:45:38.787 回答