我的 UIButtons 上的操作有点麻烦。我正在以编程方式创建 4 个按钮,但我无法触发 TouchUpInside 事件。
我已经通读了 SO,但我仍然遇到问题,所以任何指针都将不胜感激!
这是我创建和设置按钮的代码及其操作:
UIButton *btn;
float newWidth = 10;
for (int i = 0; i < _btnImages.count; ++i) {
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect imageFrame = CGRectMake(/*X*/newWidth, /*Y*/height - 80, 65, 65);
btn.frame = imageFrame;
btn.tag = i;
[btn setBackgroundImage:[UIImage imageNamed:[_btnImages objectAtIndex:i]] forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(btnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[btn setEnabled:true];
[self addSubview:btn];
newWidth = newWidth + 75;
}
这是 btnSelected 方法:
-(void)btnSelected:(UIButton *)button
{
NSLog(@"Button %ld Clicked",(long int)[button tag]);
}