我已经动态创建了视图,在动态创建的按钮内部。单击按钮时,我必须同时获取视图和按钮的标签。我已将代码用作
-(void)addButton
{
for (int j=0; j<[defaultNumberAry count]; j++) {
numberButton=[[UIButton alloc]initWithFrame:CGRectMake(n, 0, 40, 40)];
n=n+42;
[numberButton setBackgroundImage:[defaultNumberAry objectAtIndex:j] forState:UIControlStateNormal];
numberButton.tag=j;
[numberTagAry addObject:[NSString stringWithFormat:@"%d",j]];
numberButton.userInteractionEnabled = YES;
[numberButton addTarget:self action:@selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
numberButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
[numberView addSubview:numberButton];
}
}
-(void)addView:(int)yv
{
n=22;
numberView=[[UIView alloc]initWithFrame:CGRectMake(300, yv, 400, 44)];
numberView.backgroundColor=[UIColor yellowColor];
numberView.tag=b;
b++;
numberView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touched:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[numberView addGestureRecognizer:tapGestureRecognizer];
}
-(void)pressed:(id)sender{
UIButton *button = (UIButton *)sender;
if(!button.selected){
NSLog(@"selected btn tag:%d",button.tag);
}
}
- (void) touched:(id)sender
{
int v=((UIGestureRecognizer *)sender).view.tag;
NSLog(@"view tag:::%d",v);
}
有时控件会按下按钮,有时会查看触摸。我必须一次获得两个标签。提前致谢