【发布时间】:2014-02-16 12:05:12
【问题描述】:
我正在尝试使用 xib 向 UICollectionViewCell 的子视图添加手势,我正在这样做:
.h
@interface MyCell : UICollectionViewCell <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIView *containerButton;
@end
.m
@implementation MyCell
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[panGestureRecognizer setDelegate:self];
if (self.containerButton) {
NSLog(@"ok"); //not enter here
[self.containerButton addGestureRecognizer:panGestureRecognizer];
}
}
-(void)prepareForReuse {
[super prepareForReuse];
if (self.containerButton) {
NSLog(@"ok 2");
}
}
我已经创建了与xib文件连接的UICollectionViewCell子类,在这里我创建了容器按钮视图,如果我尝试在initialize方法中添加手势,containerButton为nil,所以不起作用,但是在prepareForReuse 方法不为空,我可以在那里添加手势吗?或者我可以在其他地方做吗?
【问题讨论】:
标签: ios iphone objective-c xib uicollectionviewcell