在iOS上,我通过编程创建了一个按钮,但事件没有触发。
代码放在自定义视图(而不是自定义视图控制器)中,但我无法触发事件。按下动画不显示。自定义视图启用了userInteractionEnabled。 我也尝试在同一视图上使用storyboard创建另一个按钮,那个按钮可以工作。按钮代码在initWithFrame中完成。必须是我没有发现的一些简单错误,我已经为此苦恼了数小时。
编辑:
事件处理程序:
UIButton * anyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[anyButton addTarget:self action:@selector(handleAnyButton:) forControlEvents:UIControlEventTouchUpInside];
anyButton.frame = CGRectMake(150, 350, 22, 22);
[self addSubview:anyButton];
代码放在自定义视图(而不是自定义视图控制器)中,但我无法触发事件。按下动画不显示。自定义视图启用了userInteractionEnabled。 我也尝试在同一视图上使用storyboard创建另一个按钮,那个按钮可以工作。按钮代码在initWithFrame中完成。必须是我没有发现的一些简单错误,我已经为此苦恼了数小时。
编辑:
事件处理程序:
-(void) handleAnyButton:(UIButton *)button
{
NSLog(@"handleAnybutton called");
}
编辑2: 以上按钮代码是在PKLocationsSelectionView类的[self setup]中完成的。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
这个视图是在视图控制器(PKLocSelectionViewController)中通过程序创建的,该视图控制器负责此层次结构:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGFloat fieldsHeight = 88;
UIView * view = [[PKLocationsSelectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, fieldsHeight)];
[self.view addSubview:view];
}