Reputation: 65
I am using core data to save the text that is added to the labels, each of these labels is on view that can be moved. I am currently saving the text with an entity that is a string, which works fine. How would I use core data to save the CGPoint of the views when moved, to be then loaded back into the saved position when the view is opened again.
The code is as follows.
-CGPoint firstTouchPoint;
//xd = distance between image center and my touch center
float xd;
float yd;
@implementation BlankViewController
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@synthesize name;
-(NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
(IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
if (self.name) {
[self.name setValue:self.nameTextField.text forKey:@"name"];
[self.name setValue:self.Label1.text forKey:@"label1"];
[self.name setValue:self.Label2.text forKey:@"label2"];
}
else{
// Create a new managed object
NSManagedObject *newName = [NSEntityDescription insertNewObjectForEntityForName:@"Maths" inManagedObjectContext:context];
[newName setValue:self.nameTextField.text forKey:@"name"];
[newName setValue:self.Label1.text forKey:@"label1"];
[newName setValue:self.Label2.text forKey:@"label2"];
}
- (void)viewDidLoad {
[super viewDidLoad];
if (self.name) {
[self.nameTextField setText:[self.name valueForKey:@"name"]];
[self.Label1 setText:[self.name valueForKey:@"label1"]];
[self.Label2 setText:[self.name valueForKey:@"label2"]]; }}
Code to move the views:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* bTouch = [touches anyObject];
if ([bTouch.view isEqual:[self imgTest]]) {
firstTouchPoint = [bTouch locationInView:[self view]];
xd = firstTouchPoint.x - [[bTouch view]center].x;
yd = firstTouchPoint.y - [[bTouch view]center].y;
[self.view bringSubviewToFront:self.imgTest];
}
else if ([bTouch.view isEqual:[self imgTest2]]) {
firstTouchPoint = [bTouch locationInView:[self view]];
xd = firstTouchPoint.x - [[bTouch view]center].x;
yd = firstTouchPoint.y - [[bTouch view]center].y;
[self.view bringSubviewToFront:self.imgTest2];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* mTouch = [touches anyObject];
if (mTouch.view == [self imgTest]) {
CGPoint cp = [mTouch locationInView:[self view]];
[[mTouch view]setCenter:CGPointMake(cp.x-xd, cp.y-yd)];
}
else if (mTouch.view == [self imgTest2]) {
CGPoint cp = [mTouch locationInView:[self view]];
[[mTouch view]setCenter:CGPointMake(cp.x-xd, cp.y-yd)];
}
}
Upvotes: 1
Views: 1312
Reputation: 6635
If you just need to store a single CGPoint
with your entity, I would add two required float attributes to your entity (say x
and y
) and store the point like that.
With the latest Xcode, you can have it generate model objects for you which is easier than dealing with the plain NSManagedObject
. With that model object it would also be easy to add a category that defines a point
property that you can use to convert the Core Data attributes to a CGPoint
and vice versa.
Upvotes: 2