Domanda

In my app the user is able to add an value with an UISwitch. Once the user has selected the value the, value is uploaded to the current user in parse. When the user gets back to the view where the user selected the value using the UISwitch, i want the UISwitch to be loaded the way the current user selected it last time. But i just cant get it to work.

I use this query, to get the value from the currentUser

PFQuery *query= [PFUser query];

[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
PFObject *object = [query getFirstObject];

And this is where i want to put the value in.

(void)viewDidAppear:(BOOL)animated; { 

[super viewDidAppear:animated];

PFQuery *query= [PFUser query];

[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
PFObject *object = [query getFirstObject];

NSNumber *logNSNumber = [object valueForKey: @"Active"];
bool switchValue = [logNSNumber boolValue];

BOOL switchValue;

if (switchValue)
{
    switchLog.on = TRUE;
    legoTextView.hidden = NO;    
}
else
{
    switchLog.on = FALSE;
    lTextView.hidden = YES;        
}   
}

And this is how i send the boolean value up to parse.

   -(IBAction)changeBoolean; {

        Boolean switchVaule;

        if (switchLog.on)
        {
            switchVaule = TRUE;
            logTextView.hidden = NO;

              PFUser *log = [PFUser currentUser];
                log[@"Active"] = @YES;
                 [log saveInBackground]; 
        }
        else
        {
            switchVaule = FALSE;
            logTextView.hidden = YES;

              PFUser *log = [PFUser currentUser];
                log[@"Active"] = @NO;
                 [log saveInBackground];      
            }   
        }

Does any one know how to solve this problem, is this the right way to do this, or am i trying to do this the wrong way? Thx!

È stato utile?

Soluzione

There are lots of things that are wrong in your code. For one; you are getting the PFUser object for the current user by querying for it using data from the user object you're already having ([PFUser currentUser]). When you log in, Parse gives you this object automatically, and you don't need to query for it. To store a value in it:

[[PFUser currentUser] setObject:[NSNumber numberWithBool:YES] forKey:@"Active"];
[[PFUser currentUser] saveInBackground];

to get value from it:

[[PFUser curentUser] objectForKey:@"Active"];

In your code, you're not getting the stored value anywhere to use in your switch. One solution:

switchLog.on = [[PFUser curentUser] objectForKey:@"Active"];

You have also, as jrturton points out, lots of typos in your code, and less-than-ideal condition handling. Clean that up and try using the code I provided.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top