首页 > 代码库 > UIButton点击后改变背景的等方法 按钮

UIButton点击后改变背景的等方法 按钮

请问下UIButton点击后改变背景的方法

http://www.cocoachina.com/bbs/read.php?tid-48666-keyword-UIButton.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//初始设置:
UIImage *bgImg1 = [UIImage imageNamed:@"Selected.png"];
UIImage *bgImg2 = [UIImage imageNamed:@"Unselected.png"];
[btn setImage:bgImg2 forState:UIControlStateNormal];
[btn setImage:bgImg1 forState:UIControlStateSelected];
 
//然后在button的Action方法改变button状态:
- (IBAction) buttonTouch:(id)sender
{
    UIButton *button = (UIButton *)sender;
 
    button.selected = !button.selected;
}

 

——————————————————————

批量创建button

?
1
2
3
4
5
6
7
8
9
for(int i=1; i<=6;i++) {
UIButton btn;
bt setTitle:@"btn..." forState....]
btn.tag = i;
[myview addSubview bt];
}
 
//获得button
UIButton *bt1 = (UIButton *)[myview viewWithTag:1];

 -------------------------------------

获取UIView上所有的Button的tag值

UIView上循环了很多UIButton,然后button点击后tag+100.然后我要做一个多选的形式,最后统计的时候统计tag<100的,取对应数组里的数据。如何button的tag值。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(id obj in [uiview subviews])
{
    if([obj isKindofClass:[UIButton class]])
    {
        UIButton *ButtonTmp = (UIButton *)obj;
        if(ButtonTmp.tag<100)
        {
            NSLog(@"我就是你想要的tag小于100的button");
        }
        else
        {
            NSLog(@"这些都是tag大于等于100的button")
        }
    }
}

 

-------------获取父视图tag值-------------

button.superview.tag

----------------改变button背景-----

?
1
2
3
4
5
6
7
8
9
10
11
- (void)selectChange:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    [UIView animateWithDuration:0.6 animations:^{
        if (btn.tag==1) {
            self.btnview.frame=CGRectMake(100, 100, 50, 30) ;
        }else{    
            self.btnview.frame=CGRectMake(200, 100, 50, 30) ;
        }
    } ];
}

 自定义cell中有一个button按钮,按下按钮的同时怎么获得该按钮所在的行的row值

?
1
2
3
4
5
6
//------解决方案--------------------
[(UIButton *)[cell viewWithTag:5] addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn=(UIButton *)[cell viewWithTag:5];
btn.tag=indexPath.row+100;
//------解决方案--------------------
//给button设定tag值,跟row值有固定的关系

 iPhone的九宫格实现代码

http://www.cocoachina.com/iphonedev/sdk/2010/0803/1968.html