码迷,mamicode.com
首页 > 其他好文 > 详细

保持按钮选中状态,当点击另一个按钮后之前被选中的按钮恢复原样 实现代码

时间:2015-10-13 17:10:00      阅读:1148      评论:0      收藏:0      [点我收藏+]

标签:

//可直接复制 测试

#import "ViewController.h"

@interface ViewController ()

{

    NSInteger num;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    num = -1 ;

    [self layoutView];

}

 

- (void)layoutView

{

         int totalloc=3;

    int count = 6;

         CGFloat appvieww=100;

         CGFloat appviewh=100;

    

         CGFloat margin=(self.view.frame.size.width -totalloc*appvieww)/(totalloc+1);

    

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

                 int row=i/totalloc;

             

                 int loc=i%totalloc;

        

                 CGFloat appviewx=margin+(margin+appvieww)*loc;

                 CGFloat appviewy=margin+(margin+appviewh)*row;

             UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

             button.frame = CGRectMake(appviewx, appviewy, appvieww, appviewh);

             [button setTitle:@"按钮" forState:UIControlStateNormal];

             [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

             [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

             button.tag = i +1;

             [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

             

             [self.view addSubview:button];

           

             

             }

    

    

}

 

 

- (void)click:(UIButton *)button

{

 

    UIButton *btn1 = (UIButton *)[self.view viewWithTag:num];

    

    if (num != 0)

        

    {

        

        btn1.selected = NO;

        

    }

    

    num = button.tag;

    

    button.selected = YES;

 

}

 

 

@end 

 

 

 

 

 

 

 

 

 

@end

 

保持按钮选中状态,当点击另一个按钮后之前被选中的按钮恢复原样 实现代码

标签:

原文地址:http://www.cnblogs.com/qimingtainanle/p/4874711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!