【发布时间】:2024-05-04 14:25:02
【问题描述】:
我创建了按钮数组,但我没有看到按钮处理触摸事件(buttonEvent: 不是调用)
这是我的代码 - 不正确吗?
- (void)loadView{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:screenRect];
[backgroundImageView setImage:[UIImage imageNamed:@"background.png"]];
backgroundImageView.opaque = YES;
self.view = backgroundImageView;
[backgroundImageView release];
CGRect brandRect = CGRectMake(90, 25, 140, 70);
UIImageView *brandImageView = [[UIImageView alloc] initWithFrame:brandRect];
[brandImageView setImage:[UIImage imageNamed:@"brand.png"]];
[self.view addSubview:brandImageView];
buttons = [NSMutableArray array];
int y = 100;
for ( int i = 0 ; i < 5; i++ ){
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(20, y, 280, 50)];
if ( i == 0 ){
[button setBackgroundImage:[UIImage imageNamed:@"select_active.png"] forState:UIControlStateNormal];
} else {
[button setBackgroundImage:[UIImage imageNamed:@"select_passive.png"] forState:UIControlStateNormal];
}
[button setTitle:[NSString stringWithFormat:@"Object%d",i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventAllEvents];
button.tag = 1000 + i ;
[self.view addSubview:button];
y += 60;
}
-(void)buttonEvent:(id)sender {
NSLog(@"new button clicked!!!");
}
【问题讨论】:
-
我找到了解决方案 - backgroundImageView.userInteractionEnabled = YES 解决了我的问题
标签: iphone button touch selector