我有一个 UIButton 子类,我在其中将属性设置concert
为NSManagedObject
. 当我设置这个属性时,我确定它NSManagedObject
不为空,并且它的属性不为空。此外,对象的数据显示正确的数据。
concert
当单击按钮并将其发送到方法时使用后者时sender
,我会将sender
转换为我的子类,UIButton
并且现在的属性concert
将返回 null,但其concert
本身不为 null。此外,数据将是fault
.
有谁知道为什么会发生这种情况以及我该如何解决?
在这里,我初始化我的按钮:
NFConcertButton *button = [NFConcertButton buttonWithConcert:concert tileSize:self.tileSize];
在initWithButton:tileSize:
(由静态方法调用)中,我存储了 Concert 以供以后使用。
- (id)initWithConcert:(NFConcert *)concert tileSize:(CGSize)tileSize
{
if (self = [super init])
{
// Store concert
_concert = concert;
/*
<NFConcert: 0xde67eb0> (entity: Concert; id: 0xde63f60 <x-coredata://F027F762-2F30-4A43-898B-42ECC199DE97/Concert/p2> ; data: {
band = "SomeBand";
})
*/
// concert is not null
// concert.band is not null
// .... //
}
return self;
}
按下按钮时,将调用以下方法,concert
并且现在的属性为空。
- (void)concertButtonPressed:(id)sender
{
NFConcert *concert = ((NFConcertButton *) sender).concert;
// <NFConcert: 0xde67eb0> (entity: Concert; id: 0xde63f60 <x-coredata://F027F762-2F30-4A43-898B-42ECC199DE97/Concert/p2> ; data: <fault>) => (null)
// concert is not null.
// concert.band is now null.
}
更新:
如果我NSManagedObject
再次使用获取objectId
,我将获取数据并且属性不会返回 null。我不明白为什么这是必要的。谁能告诉我?
以下将起作用。
NFConcert *concert = ((NFConcertButton *) sender).concert;
concert = (NFConcert *) [managedObjectContext existingObjectWithID:concert.objectID error:nil];