2

This question has probably been asked before so I'm sorry.

I am working on an iPhone app and lets say I have a variable, var, in class1. I want to add a UIButton in class2 which when hit resets the var to 0. The var value is saved when the app closes so when the view loads var is assigned with the value (I assume you guys could have figured that out but figured I would clarify the code). How can I write something like this? Here's basically what I tried:

@interface Class1 { 
    double var;
}
-(void)resetVar;
@end

@implementation Class1
-(void)viewDidLoad {
    NSUserDefaults *loadCount = [NSUserDefaults standardUserDefaults];
    var = [loadNoCount doubleForKey:@"count"];
}
-(void)resetVar {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"count"];
    var = 0;
}


@interface Class2 { 
    Class1 *classObj;
}
@property(nonatomic, retain) Class1 *classObj;
-(IBAction)reset:(id)sender;
@end

@implementation Class2

-(void)viewDidLoad {
    classObj = [[Class1 alloc] init];
}
-(IBAction)reset:(id)sender {
    [classObj resetVar];
}
@end
1
  • I've tried a setter method as well. The var wasn't getting the value until after the app was closed and reopen for some reason? Commented Nov 6, 2010 at 0:26

2 Answers 2

1

Use a setter method -setVar: or similar on the class that you want to set the variable in. You can't access instance variables of other objects: they are all private by default.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem with the snippet of code you're showing is that NSUserDefaults only writes the values down to disk when the app quits (or probably also when it's sent to the background in iOS 4). If you need to force NSUserDefaults to write the values of the keys to disk, call the [[NSUserDefaults standardUserDefaults] synchronize] method on it.

Also, instead of using removeObjectForKey: in resetVar, why not using setDouble:forKey: instead? This way you could also get rid of the "double var" ivar in Class1, just using NSUserDefaults as store of the data.

Here's a bit of info about the synchronize method, from the NSUserDefaults documentation:

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

This is how I would implement what you described in your code:

@interface Class1 
{
}
- (void)resetVar;
@end

@implementation Class1
- (void)viewDidLoad 
{
    [self resetVar];
}
- (void)resetVar 
{
    [[NSUserDefaults standardUserDefaults] setDouble:0.0 forKey:@"count"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

@interface Class2 
{ 
    Class1 *classObj;
}
- (IBAction)reset:(id)sender;
@end

@implementation Class2
- (void)dealloc 
{
    [classObj release];
}
- (void)viewDidLoad 
{
    classObj = [[Class1 alloc] init];
}
- (IBAction)reset:(id)sender 
{
    [classObj resetVar];
}
@end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.