Reputation: 611
I am moving images around in my app which is working fine. The problem is that I'd like to keep the images in place when the user comes back to that view. At present, I am using a string to hold the image centre:
NSString *centre = NSStringFromCGPoint(image.center);
I have an NSUserDefault variable called theCentre:
theCentre = [NSUserDefaults standardUserDefaults];
which I try to save using:
[theCentre setObject:centre forKey:@"centre"];
When I output the log, I get the following:
{280, 320}
which is great.
In the viewDidLoad, I have tried the exact same log and it is returning (null). I take it that this means it isn't saving theCentre properly. Am I missing something fundamental here because I have used NSUserDefaults elsewhere in my app and it has been working ok. Many thanks in advance.
Edit: this is the whole code to try and aid debugging :)
-(void)getBounds
{
lowerBoundX = 250;
upperBoundX = 310;
lowerBoundY = 290;
upperBoundY = 350;
}
#pragma mark -
#pragma mark UIPanGestureRecognizer selector
- (void) dragGesture:(UIPanGestureRecognizer *) panGesture
{
[self getBounds];
CGPoint translation = [panGesture translationInView:self.view];
switch (panGesture.state) {
case UIGestureRecognizerStateBegan:{
originalCentre = dragImage.center;
}
break;
case UIGestureRecognizerStateChanged:{
dragImage.center = CGPointMake(dragImage.center.x + translation.x,
dragImage.center.y + translation.y);
}
break;
case UIGestureRecognizerStateEnded:{
if (((dragImage.center.x >= lowerBoundX) && (dragImage.center.y >= lowerBoundY) &&
(dragImage.center.x <= upperBoundX) && (dragImage.center.y <= upperBoundY))) {
dragImage.center = CGPointMake(280, 320);
[dragImage setUserInteractionEnabled:NO];
theCentre = [NSUserDefaults standardUserDefaults];
NSString *centre = NSStringFromCGPoint(dragImage.center);
[theCentre setObject:centre forKey:@"centre"];
NSLog(@"%@", [theCentre objectForKey:@"centre"]);
[theCentre synchronize];
break;
}
[UIView animateWithDuration:0.5
animations:^{
dragImage.center = originalCentre;
}
completion:^(BOOL finished){
}];
}
break;
default:
break;
}
[panGesture setTranslation:CGPointZero inView:self.view];
}
-(IBAction)clear{
[UIImageView animateWithDuration:0.5 animations:^{
dragImage.center = CGPointMake(100, 200);
}];
[dragImage setUserInteractionEnabled:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"%@", [theCentre objectForKey:@"centre"]);
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragGesture:)];
[dragImage addGestureRecognizer:panGesture];
[dragImage setUserInteractionEnabled:YES];
}
Upvotes: 0
Views: 415
Reputation: 4715
Data saved in NSUserDefaults is first cached and thus it is not available instantly from other instances of the NSUserDefaults. The data is saved periodically on some predefined events/time intervals. However you can force a data synchronization by calling synchronize like this:
NSString *theCenter = NSStringFromCGPoint(image.center);
theCenter = [NSUserDefaults standardUserDefaults];
[theCenter setObject:centre forKey:@"center"];
[theCenter synchronize];
Upvotes: 2