如果核心数据有一个具有特定成员变量值的对象,则我将一个按钮绘制到视图中;同时屏幕上可能有任意数量的这些按钮。
如果用户单击该按钮,我希望获得相关的Core数据对象。
,在允许按钮引用/调用核心数据对象方面,允许这种情况发生的最佳方式是什么?
我有一些自己的想法,但想看看是否有更快/更有效的方法。
编辑:
1. 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.
2. When I create the button I store 4-5 variables (Subclassing `UIButton`) that will allow a predicate to find the associated object in Core data.
3. Storing active button pointers in a dictionary with the CoreData ID
我赞成备选案文1,有什么想法或备选办法吗?
发布于 2011-02-03 15:15:18
好吧,我选择了选择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;
@endCoreDataAwareButton.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发布于 2011-01-31 19:34:35
您是否考虑过在视图控制器(或负责创建/删除按钮的任何东西)中使用KVO来观察感兴趣的变量?
在这方面,你已经考虑过什么方法?这可能会使你的问题更容易得到别人的反馈。
发布于 2011-01-31 20:14:16
虽然您没有提供太多的细节,但我的建议是在核心数据对象中找到一个唯一的属性,您可以使用该属性来处理按钮的标识符属性。我可能会使用托管对象id或URI表示。
然后,在按钮处理程序方法中,您可以提取(Id)发送方的标识符,这将使您能够找到“属于”该按钮的特定托管对象。
https://stackoverflow.com/questions/4853773
复制相似问题