UI代码有效,但更改为XIB失败

时间:2015-05-28 14:00:09

标签: ios objective-c iphone

以下代码可以使用

@interface MyUICollectionViewCell : UICollectionViewCell {


}
@implementation UICollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {


        self = [super initWithFrame:frame];
        if (self) {

            _moreView = [[UIView alloc] init];
            _moreView.backgroundColor = [UIColor clearColor];
            _moreView.frame = CGRectMake(self.frame.size.width - 70, 0, 45, 37);
            [self addSubview:_moreView];
            _moreView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goToDoSomething)];
            [_moreView addGestureRecognizer:tapMoreView];



        }
        return self;
    }
    return self;
}

- (void)doSomeThing
{ 

}

当我触摸子视图(灰色块)时,它会触发事件, 但我改为XIB(子视图链接到IBOutlet mUIView),并更改下面的代码,goToDoSomething无法触发。

您的评论欢迎

enter image description here

@interface MyUICollectionViewCell : UICollectionViewCell {

    IBOutlet UIView *mUIView;
}
@implementation UICollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self = [super initWithFrame:frame];
        if (self) {


            NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"MyUICollectionViewCell"owner:self options:nil];
            UIView *bw = [nibView objectAtIndex:0] ;


            bw.userInteractionEnabled = YES;

            [self.contentView addSubview:bw];
            mUIView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goToDoSomething)];
            [mUIView addGestureRecognizer:tapMoreView];

        }
        return self;
    }
    return self;
}
- (void)doSomeThing
{ 

}

0 个答案:

没有答案