Chase
Chase

Reputation: 11

NSUserDefaults - Saving UIImageView Position

In my application, I have a UIImageView and a UIButton. The UIImageView has a UIPanGestureRecognizer, and can be dragged around the screen. I have written:

int defaultBallLocation = ball.center; in my - (IBAction)saveButton

and Xcode is saying "Cannot associate CGPoint with int".

So, how can I save the ball (UIImageView) position on the screen by pressing "save", then load the saved position by pressing "load" through NSUserDefaults? I have worked with NSUserDefaults before, but only with a UISegmentedController. Thanks!

Upvotes: 1

Views: 1274

Answers (2)

Kjuly
Kjuly

Reputation: 35171

Another option, you can store it as string:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
// Set
[userDefaults setObject:NSStringFromCGPoint(yourPoint) forKey:@"pointPosition"];
// Save
[userDefaults synchronize];
// Get
CGPoint thePoint = CGPointFromString([userDefaults objectForKey:@"pointPosition"]);

Upvotes: 3

Peter Warbo
Peter Warbo

Reputation: 11728

You are trying to assign an int with a CGPoint struct which is not possible.

To save it into NSUserDefaults you could do:

CGFloat x = ball.center.x;
CGFloat y = ball.center.y;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:x forKey:@"x"];
[defaults setFloat:y forKey:@"y"];

Upvotes: 1

Related Questions