ani*_*ani 0 objective-c ios xcode4
我已经动态创建了视图,并在其中动态创建了按钮。单击按钮时,我必须同时获取视图和按钮的标签。我已经将代码用作
-(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);
}
Run Code Online (Sandbox Code Playgroud)
有时控件要按下按钮,有些时候要查看触摸。我必须一次获得两个标签。提前致谢
当您添加子视图时,如下所示:
[numberView addSubview:numberButton];
Run Code Online (Sandbox Code Playgroud)
numberButton成为子视图的numberView; 也numberView成为上海华盈的numberButton。您可以通过该属性访问它。
-(void)pressed:(id)sender{
UIButton *pressedButton = (UIButton *)sender;
UIView *superViewOfPressedButton = pressedButton.superview;
NSLog(@"Tag of button:%i Tag of pressed button's button view is %i",pressedButton.tag,superViewOfPressedButton.tag);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3797 次 |
| 最近记录: |