首页 > 其他 > 详细

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

时间:2015-10-13 17:07:09      阅读:411      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!