首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UI对象->核心数据关联(子类UIButton,添加实例变量)

UI对象->核心数据关联(子类UIButton,添加实例变量)
EN

Stack Overflow用户
提问于 2011-01-31 17:08:46
回答 3查看 546关注 0票数 1

如果核心数据有一个具有特定成员变量值的对象,则我将一个按钮绘制到视图中;同时屏幕上可能有任意数量的这些按钮。

如果用户单击该按钮,我希望获得相关的Core数据对象。

,在允许按钮引用/调用核心数据对象方面,允许这种情况发生的最佳方式是什么?

我有一些自己的想法,但想看看是否有更快/更有效的方法。

编辑:

  • 按钮创建将始终遵循托管对象的创建。
  • 按钮的创建也可以通过读取Core数据来触发,所以托管对象的创建并不总是会进行按钮创建。
代码语言:javascript
复制
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,有什么想法或备选办法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-02-03 15:15:18

好吧,我选择了选择1.

创作:

代码语言:javascript
复制
    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");

关于新闻:

代码语言:javascript
复制
-(void)pressCoreDataAwareButton:(id)sendr
{       
    CoreDataAwareButton * note = (CoreDataAwareButton *)sendr;  

    SomeManagedObject * obj = (SomeManagedObject*)[fetchObjectFromCoreDataWithID:note.coreDataIDentifier];
}

CoreDataAwareButton.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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
票数 0
EN

Stack Overflow用户

发布于 2011-01-31 19:34:35

您是否考虑过在视图控制器(或负责创建/删除按钮的任何东西)中使用KVO来观察感兴趣的变量?

在这方面,你已经考虑过什么方法?这可能会使你的问题更容易得到别人的反馈。

票数 1
EN

Stack Overflow用户

发布于 2011-01-31 20:14:16

虽然您没有提供太多的细节,但我的建议是在核心数据对象中找到一个唯一的属性,您可以使用该属性来处理按钮的标识符属性。我可能会使用托管对象id或URI表示

然后,在按钮处理程序方法中,您可以提取(Id)发送方的标识符,这将使您能够找到“属于”该按钮的特定托管对象。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4853773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档