动态添加UIButton控件,通过设置tag值实现点击不同的UIButton控件做出不同的反应
<div class="postcontent"><div id="cnblogs_post_body">在上一篇博文《在滚动视图里添加图像视图,在图像视图里添加按钮控件》的基础上做了小小的改动。http://pic002.cnblogs.com/images/2012/266130/2012120622203750.png》》点击button1》》http://pic002.cnblogs.com/images/2012/266130/2012120622210755.png
》》点击button2》》
http://pic002.cnblogs.com/images/2012/266130/2012120622214177.png
需要改写两个地方:
<div class="cnblogs_code">@synthesize scrollView = _scrollView;- (void)viewDidLoad{ ; // 配置 UIImageView 对象 UIImageView *imgView = [ initWithImage:@"1.png"]]; imgView.userInteractionEnabled = YES;// UIImageView 的 userInteractionEnabled 属性默认 "NO",因此默认情况下,添加在 UIImageView 中的 UIButton 将不发生触摸事件 // 配置并添加 UIButton 对象 UIButton *button1 = ; button1.frame = CGRectMake(100, 80, 40, 40); @"Button1" forState:UIControlStateNormal]; button1.tag = 100; // 1、分别设置tag ; ; UIButton *button2 = ; button2.frame = CGRectMake(200, 80, 40, 40); @"Button2" forState:UIControlStateNormal]; button2.tag = 200; // 1、分别设置tag ; ; // 添加 UIImageView 对象 ; // 配置 UIScrollView 对象 self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack; self.scrollView.scrollEnabled = YES; self.scrollView.clipsToBounds = YES; self.scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height); }- (IBAction)button:(id)sender{ UIButton *button = (UIButton *)sender; // 2、通过tag进行选择对应的操作 if (button.tag == 100) { @"SeguePush" sender:self]; } else { NSLog(@"button2"); }}
页:
[1]