UI object -> Core Data association (Subclassing UIButton, adding instance variable)
If core data has an object that has a specific member variable value I draw a button to a view; there could be any number of these buttons on screen at the same time.
If the user clicks that button I want to get that associated Core data object.
What would be the best way allow this to happen in terms of allowing the button to reference/call the core Data object?
I have a few ideas of my own, but want to see if there is any quicker/more efficient methods.
Edit:
- Button creation will always follow the creation of a managed object.
Buttons creation can also be triggered by a Core data read so Not always will the Managed object creation proceed button creation.
When I create the button, I save it to make sure it was a non-temperory UID, read its UID and store it in a varible (Subclassing
UIButton
). (The creation Process is optional, note the 2 bullets above). This idea is what ccjensen is getting at.When I cr开发者_Go百科eate the button I store 4-5 variables (Subclassing
UIButton
) that will allow a predicate to find the associated object in Core data.Storing active button pointers in a dictionary with the CoreData ID
I would favour option 1, any thoughts or alternatives?
Have you considered using KVO with view controller (or whatever is responsible for creating/deleting buttons) observing the variable of interest?
For that matter, what approaches have you already considered? This might make your question more inviting to feedback from others.
Although you don't provide much details, my immediate suggestion would be to find a unique property in the core data objects that you can use for the buttons 'identifier' property. I would probably use the managed objects id or URI representation.
Then in your button handler method you can pull out the identifier of the (id)sender and that should give you the means to find the specific managed object that "belongs" to that button.
Ok, I went with Option 1...
Creation:
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");
On Press:
-(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
精彩评论