Хорошо, я выбрал вариант 1 ...
Создание:
SomeManagedObject * managedObj = (SomeManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"SomeManagedObject" inManagedObjectContext:myManagedContext];
NSError * er = nil;
if(![myManagedContext save:&er])NSLog(@"ERROR:SAVE Error -%@",er);
NSManagedObjectID *identifier = [managedObj objectID];
CGPoint myPoint;//set point data
if(![identifier isTemporaryID]){
CoreDataAwareButton * button = [CoreDataAwareButton buttonWithPosition:myPoint CoreDataId:identifier AndDelegate:self];
[self.documentImage button];
}
else NSLog(@"Error-save error");
При печати:
-(void)pressCoreDataAwareButton:(id)sendr
{
CoreDataAwareButton * note = (CoreDataAwareButton *)sendr;
SomeManagedObject * obj = (SomeManagedObject*)[fetchObjectFromCoreDataWithID:note.coreDataIDentifier];
}
CoreDataAwareButton.h
#import <Foundation/Foundation.h>
@interface CoreDataAwareButton : UIButton {
NSManagedObjectID * _coreDataIDentifier;
}
@property(nonatomic,retain) NSManagedObjectID * coreDataIDentifier;
+(AnnotationButton*)buttonWithPosition:(CGPoint)point CoreDataId:(NSManagedObjectID*)identifier AndDelegate:(id)del;
@end
CoreDataAwareButton.m
#import "CoreDataAwareButton.h"
#import <objc/runtime.h>
@implementation CoreDataAwareButton
@synthesize coreDataIDentifier=_coreDataIDentifier;
+(CoreDataAwareButton*)buttonWithPosition:(CGPoint)point CoreDataId:(NSManagedObjectID*)identifier AndDelegate:(id)del{
CoreDataAwareButton* button = [self buttonWithType:UIButtonTypeCustom];
if (button && (class_getInstanceSize([button class]) == class_getInstanceSize([CoreDataAwareButton class]))) {
button->isa = [CoreDataAwareButton class];//This looks dangerous, but its fine; credit: http://blog.jayway.com/2008/12/12/uibutton-troubles-and-obj-c-magic/
[button addTarget:del action:@selector(pressCoreDataAwareButton:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(point.x, point.y, 30, 30);
//button.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:@"annotation_icon_large.png"];
[button setImage:img forState:UIControlStateNormal];
button.coreDataIDentifier = identifier;
}
return button;
}
-(void)dealloc{
[_coreDataIDentifier release];_coreDataIDentifier=nil;
[super dealloc];
}
@end