Iphone 数组中的UIButton处理
我创建了一个按钮数组,但我看不到按钮处理触摸事件(buttonEvent:not调用) 这是我的代码-不正确吗Iphone 数组中的UIButton处理,iphone,button,touch,selector,Iphone,Button,Touch,Selector,我创建了一个按钮数组,但我看不到按钮处理触摸事件(buttonEvent:not调用) 这是我的代码-不正确吗 - (void)loadView{ CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:screenRect]; [backgroundImageView setImage:[
- (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!!!");
}
-(无效)加载视图{
CGRect screenRect=[[UIScreen mainScreen]applicationFrame];
UIImageView*backgroundImageView=[[UIImageView alloc]initWithFrame:screenRect];
[backgroundImageView setImage:[UIImage ImageName:@“background.png”];
backgroundImageView.不透明=是;
self.view=backgroundImageView;
[背景图像视图发布];
CGRect-brandRect=CGRectMake(90,25,140,70);
UIImageView*brandImageView=[[UIImageView alloc]initWithFrame:brandRect];
[brandImageView setImage:[UIImage ImageName:@“brand.png”];
[self.view addSubview:brandImageView];
按钮=[NSMutableArray];
int y=100;
对于(int i=0;i<5;i++){
UIButton*button=[UIButton button类型:UIButtonTypeCustom];
[按钮设置框:CGRectMake(20,y,280,50)];
如果(i==0){
[按钮setBackgroundImage:[UIImage ImageName:@“select_active.png”]用于状态:UIControlStateNormal];
}否则{
[按钮setBackgroundImage:[UIImage ImageName:@“select_passive.png”]用于状态:UIControlStateNormal];
}
[按钮集标题:[NSString stringWithFormat:@“对象%d”,i]用于状态:UIControlStateNormal];
[按钮设置标题颜色:[UIColor blueColor]用于状态:uicontrol状态正常];
[按钮添加目标:自我操作:@选择器(按钮事件:)用于控制事件:UIControlEventAllEvents];
button.tag=1000+i;
[self.view addSubview:按钮];
y+=60;
}
-(无效)按钮事件:(id)发件人{
NSLog(@“单击新按钮!!!”);
}
您正在向uiimageview添加按钮,uiimageview已禁用交互功能
此外,您没有按钮数组,因为按钮是自动删除的,并且您从未将对象添加到按钮数组中。我找到了解决方案-backgroundImageView.userInteractionEnabled=是解决了我的问题