我创建了一个名为UICustomButton
的类,它是UIView
的子类。我将一个UIButton
添加到UIView
中作为subview
,如下所示:
-(id)initWithButtonType:(NSString *)type
{
self = [super init];
if (self)
{
self.customButton = [self setupButtonWithTitle:type andFrame:frame];
[self addSubview:self.customButton];
self.customButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
我面临的问题是,虽然按钮出现了,但它们无法被点击。我将按钮添加到
UIView
的方式有问题吗?注:补充一下,我正在将这个自定义按钮类实例添加到单元格中。
UICustomButton *customButton = [[UICustomButton alloc]initWithFrame:someFrame];
[cell.contentView addSubView:customButton];
setupButtonWithTitle:andFrame:
是做什么的?此外,您需要确保如果initWithButtonType:
的代码引用了self
来添加一个选择器buttonPressed
,那么buttonPressed
方法确实在同一类中实现。 - user244343