button点击问题

来源:3-4 栏目分组模块标题按钮的边框

盛益华通

2018-07-13 10:04:25

-(void)addScrollView{

    scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 414, 40)];

    scroll.contentSize=CGSizeMake(100*newsarray.count, 40);

    scroll.contentOffset=CGPointMake(0, 0);//使当前页面显示在第一页

    scroll.backgroundColor=[UIColor magentaColor];

    scroll.showsHorizontalScrollIndicator=false;//隐藏水平方向的光标

    for (int i = 0; i<newsarray.count; i++) {

        UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(i*100, 0, 100, 50)];

        NSString *btnTitle =[newsarray objectAtIndex:i];//将数组里的内容赋给i  objectAtIndex通过索引获取对象

        [btn setTitle:btnTitle forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        //添加响应方法

        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

        btn.tag=10000+i;//给button按钮加标签 便于区分

        [scroll addSubview:btn];

        /*

        if (i==0) {

            btn.layer.borderColor=[UIColor whiteColor].CGColor;

            btn.layer.borderWidth=2.0;

            btn.layer.cornerRadius=5.0;

        }*/

    }

    [self addSubview:scroll];

}

//实现button按钮的响应方法:点击button时会有点击效果

-(void)btnAction:(UIButton*)button{

    button.layer.borderColor=[UIColor whiteColor].CGColor;

    button.layer.borderWidth=2.0;

    button.layer.cornerRadius=5.0;

    

    for (UIButton*items in scroll.subviews) {

        if (items.tag>=10000 && items.tag<=10000+newsarray.count&&(items.tag!=button.tag)) {

            button.layer.borderWidth=0;

        }

    }

}

加上scroll视图遍历之后 点击方法就实现不了了为什么 跟老师的代码一样啊

写回答

1回答

imoocLovers

2018-07-13

首先检查下运行成功后添加在scrollView上的button是否正常显示,将[self addSubview:scroll];放在生成button的for循环的前面试一下。祝学习愉快~

0

0 学习 · 637 问题

查看课程