button實現類似uisegment效果

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

    {

        //循環添加button

        UIButton * button = [custombutton initbutton:CGRectMake(i*80, 0, 80, 30) title:[[array objectAtIndex:i] valueForKey:@"typeName"]font:12 image:@"buttonCommon.png" select:@selector(touch:) who:self];

        //button添加tag

        button.tag=i+1;

        if (button.tag==1)

        {

            [button setBackgroundImage:[UIImage imageNamed:@"buttonSelect.png"] forState:UIControlStateNormal];

        }

        [scro addSubview:button];

    }



                         


 

-(void)touch:(id)sender

 

{  UIButton * button = (UIButton*)sender;

           //點擊button更改背景

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

            {   //通過tag值取到button

                UIButton * butt =(UIButton *)[scro viewWithTag:i+1];

                [butt setBackgroundImage:[UIImage imageNamed:@"buttonCommon.png"] forState:UIControlStateNormal];

            }

           [button setBackgroundImage:[UIImage imageNamed:@"buttonSelect.png"] forState:UIControlStateNormal];    

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章