- (void) createButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 60, 80, 40);
[button setTitle:@"扫按1" forState:UIControlStateNormal];
[button setTag:22];
[self.view addSubview:button];
//[button addTarget:self action:@selector(ClickEvent) forControlEvents:UIControlEventTouchUpInside];
//注意按钮的回调事件
[button addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
//下面测试多个button调用同一个事件,类似Adnroid中的事件类
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(20, 100, 80, 40);
[button2 setTitle:@"按钮2" forState:UIControlStateNormal];
[self.view addSubview:button2];
// clickevent include muitle arges
[button2 addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTag:21];
}
//回调事件
- (void) ClickEvent
{
NSLog(@"button click event......");
}
- (void) ClickEvent:(UIButton *) button
{
NSUInteger BtnTag = [button tag];
if(BtnTag == 21)
{
NSLog(@"\nbutton is:button2");
}
else
{
NSLog(@"\nbutton is:button1");
}
}
iOS中按钮根据tag来区分
最新推荐文章于 2018-04-18 15:36:23 发布