I have dynamically created view ,inside that dynamically created buttons.while clicking the buttons I have to get both view’s and button’s tag.
I have used the code as
-(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);
}
some times the control goes to button press and some times to view touched.I have to get both tag at a time.
Thanks in advance
When you add a subview like so:
numberButtonbecomes a subview ofnumberView; alsonumberViewbecomes the superview ofnumberButton. And you can access it through that property.