1

我有一个UICollectionViewCell(让我们称之为c)的实例。 c有一个child类型的属性B*

@property (strong) B* child;

里面有B一个声明

@property (strong) C* parent;

C.m我设置

self.child.parent = self

B.m我有代码:

position = self.parent.center.x;

由于某种原因,我无法center从实例外部访问 UIVIew 的属性。是私人的吗?我查看UIView.h了文档。我不认为它是私人的。

访问self.parentB.m给我正确的价值观......

那为什么我不能访问它?在C.m

self.center

正在按预期工作...

编辑:使用真实代码

这就是所谓的“Ch”

#import <UIKit/UIKit.h>
#import "UIMovableImage.h"

@interface LetterCollectionCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIImageView *letterCellView;

@end

这是“Bh”

#import <UIKit/UIKit.h>

@class LetterCollectionCell;

@interface UIMovableImage : UIImageView
{
    CGPoint currentPoint;
}
@property (strong) LetterCollectionCell* parent;

@end

这是“厘米”

#import "LetterCollectionCell.h"
#import "LettersCollection.h"

@implementation LetterCollectionCell

-(void)PrepareImage:(int)index Hint:(BOOL)hint Rotate:(BOOL)rotate
{
    if ([_letterCellView respondsToSelector:@selector(parent)])
    {
       UIMovableImage* temp = ((UIMovableImage*)self.letterCellView);
       temp.parent = self;
    }     
}    

@end

这是“Bm”

#import "UIMovableImage.h"
#import "LetterCollectionCell.h"

@implementation UIMovableImage

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
        // Get active location upon move
        CGPoint activePoint = [[touches anyObject] locationInView:self.parent];

        // Determine new point based on where the touch is now located
        CGPoint newPoint = CGPointMake(self.parent.center.x + (activePoint.x - currentPoint.x),
                                       self.parent.center.y + (activePoint.y - currentPoint.y));

 }
@end

请注意,LetterCollectionCell 的 letterCellView 是 UIImageView 类型,而不是 UIMovableImage 类型。原因是我想将此声明保留为占位符。在 Interface Builder 中,我有两个使用 LetteCollection 的场景。在一个场景中,我将 imageview 设置为 UIMovableImage(通过 Inspector Window),而在另一个场景中,我将图像设置为 UIImageView 类型。因此,运行时将在不同的场景中创建适当的类,并在我检查的集合中:如果图像具有“父级”属性 - 我设置它。否则我不会。它工作正常,分配工作正常......但访问权限不是

4

3 回答 3

4

您无法访问该属性的唯一原因是该类不知道它(前向声明)。因此,您必须#import "C.h"在 B 的实现文件中丢失。

也许需要一个例子来安抚反对者,所以让我们用你的:

这是B的标题应该是什么样子

//Give us the ability to subclass UICollectionViewCell without issue
#import <UIKit/UIKit.h>

//forward declare a reference to class B.  We cannot access it's properties until we
//formally import it's header, which should be done in the .m whenever possible.
@class B;

@interface C : UICollectionViewCell

//Declare a child property with a strong reference because we are it's owner and creator.
@property (strong, nonatomic) B* child;

@end

现在是 C 的标题。

//import foundation so we can subclass NSObject without a problem.
#import <Foundation/Foundation.h>

//Forward-declare classe C so we don't have to #import it here
@class C;

@interface B : NSObject

//keep an unretained reference to our parent because if the parent and the child have
//strong references to each other, they will create a retain cycle.
@property (unsafe_unretained, nonatomic) C* parent;

@end

现在已经不碍事了,这是您最有可能使用的 C 的实现:

#import "C.h"
#import "B.h"

@implementation C

-(id)init {
    if (self =  [super init]) {
        self.child = [[B alloc]init];
        self.child.parent = self;
    }
    return self;
}

@end

还不错,但问题是:

#import "B.h"

@implementation B

-(id)init {
    if (self = [super init]) {
        CGFloat position = self.parent.center.x; //ERROR!
    }
    return self;
}

@end

因为 C 唯一声明它有一个center属性(或者更确切地说它的超类有一个center属性),并且 C 的类型在 Ch 文件中,所以 B 在没有显式#importCh的情况下不知道 C 的属性

于 2012-10-31T22:52:47.133 回答
1

我找到了原因。整个问题都是对着错误的树吠叫。事实上,错误在另一个地方,我只是一直在调试器的监视窗口中查看“无效表达式”。

总结一下:在原始问题中描述的情况下,访问“中心”属性只是找到了。调试器的“无效表达式”和另一个地方的错误让我追逐了一个错误的东西。

我 +1d 一般正确的答案和评论,但不能接受它们,因为它可能会误导人们。

感谢大家的帮助和努力

于 2012-11-01T20:27:07.520 回答
-1

根据您的问题的顺序,在我看来,您从设置 Cm 开始,但在该行代码上,子实例尚未设置.. self.child = nil 所以设置 self.child.parent = self 会给你没有结果...因此在 Bm 上将无法访问父对象...

如果我是正确的,只需在设置 self.child.parent = self 之前设置您的子实例对象 - self.child = B* ---

于 2012-10-31T22:49:03.320 回答