对象属性不会传递给委托

时间:2011-07-09 12:39:27

标签: objective-c ios delegates

我在一个.h文件中定义了一个托管对象:

@interface MyItem : NSManagedObject {
NSString *itemName;
NSInteger itemType;
}

@property (nonatomic, retain) NSString *itemName;
@property NSInteger itemType;

@end

然后我有一个uiViewController,它有一个协议定义:

@protocol uiViewControllerDelegate <NSObject>
- (void) controllerIsDone:(MyItem *) myItem;
@end

通常,在实现uiViewControler类的某个地方我调用这个协议:

NSLog(@"Self item has its properties %@ and %i", 
    self.myItem.itemName, self.myItem.itemName);

[[self delegate] controllerIsDone:self.myItem];

我有一个tableViewController,它是这个uiViewController的委托。

我有一个tableViewController

@interface tableViewController : UITableViewController <uiViewControllerDelegate>

此tableViewController实现协议方法:

- (void) controllerIsDone:(MyItem *) myItem {
    NSLog("This is what I received %@ and %i", myItem.itemName, myItem.itemType);
}

你能告诉我,为什么在Console我得到这个:

Self item has its properties SampleName and 1
This is what I received  and 0

也就是说,虽然我将uiViewController的self.item传递给tableViewController,但它没有提供所有属性。为什么?我应该如何传递所有myItem对象,以便可以使用在uiViewController的myItem属性中设置的属性?

2 个答案:

答案 0 :(得分:0)

您是否保留myItem?在传递它之前以及收到它时检查myItem的指针值:

NSLog(@"Self item has its properties %@ and %i, and pointer value %p", 
      self.myItem.itemName, self.myItem.itemName, self.myItem);

- (void) controllerIsDone:(MyItem *) myItem {
    NSLog("This is what I received %@ and %i, and pointer value %p", myItem.itemName, myItem.itemType,myItem);
}

如果它们不相同(或在第二种情况下为空),则可能未正确保留。

答案 1 :(得分:0)

更改

@interface MyItem : NSManagedObject 

@interface MyItem : NSObject

修复了一切。

相关问题