zoukankan      html  css  js  c++  java
  • 动态添加UIButton控件,通过设置tag值实现点击不同的UIButton控件做出不同的反应

    在上一篇博文《在滚动视图里添加图像视图,在图像视图里添加按钮控件》的基础上做了小小的改动。

    》》点击button1》》

    》》点击button2》》

    需要改写两个地方:

    @synthesize scrollView = _scrollView;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        // 配置 UIImageView 对象
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
        imgView.userInteractionEnabled = YES;  // UIImageView 的 userInteractionEnabled 属性默认 "NO",因此默认情况下,添加在 UIImageView 中的 UIButton 将不发生触摸事件
        
        // 配置并添加 UIButton 对象
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button1.frame = CGRectMake(100, 80, 40, 40);
        [button1 setTitle:@"Button1" forState:UIControlStateNormal];
        
        button1.tag = 100;      // 1、分别设置tag
        
        [button1 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        [imgView addSubview:button1];
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button2.frame = CGRectMake(200, 80, 40, 40);
        [button2 setTitle:@"Button2" forState:UIControlStateNormal];
        
        button2.tag = 200;      // 1、分别设置tag
        
        [button2 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        [imgView addSubview:button2];
        
        // 添加 UIImageView 对象
        [self.scrollView addSubview:imgView];
        
        // 配置 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) {
            [self performSegueWithIdentifier:@"SeguePush" sender:self];
        } else {
            NSLog(@"button2");
        }
    }


    /**************************************************************************
                      原文来自博客园——Submarinex的博客: www.cnblogs.com/submarinex/               
      *************************************************************************/

  • 相关阅读:
    bootstrap-table对前台页面表格的支持
    解决拦截器对ajax请求的的拦截
    jQuery获取鼠标事件源(万能)
    HtmlAgilityPack 处理通配的contains
    【转】XPath的学习
    在IIS服务器上部署svg/woff/woff2字体
    【转】万网域名查询接口(API)的说明
    html5和css3的常用参考网
    【转】Xml序列化
    C#序列化s实体类成Xml,去除空格、换行符以及命名空间
  • 原文地址:https://www.cnblogs.com/submarinex/p/2806269.html
Copyright © 2011-2022 走看看