1

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)];
    }
}
knowles3226
  • 65
  • 1
  • 7

1 Answers1

2

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.

Dave Weston
  • 6,527
  • 1
  • 29
  • 44
  • 1
    Having a computed property works ok when it is a getter, e.g. when fullName is a getter for first + last names, but when you have to support a setter problems arise. Computed properties require a values affecting keys method which is fine for the getter but for the setter it causes a KVO notification for the point property when both the x and then the y is changed and then you have to try to coalesce notifications so you aren't updating things twice every time a point is changed which is very painful. If you get past that then you'll have more problems with undo and child contexts. – malhal Sep 30 '20 at 10:01