【代码笔记】iOS-4个可以单独点击的button_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 【代码笔记】iOS-4个可以单独点击的button

【代码笔记】iOS-4个可以单独点击的button

 2017/8/7 11:31:19  弦外雨  程序员俱乐部  我要评论(0)
  • 摘要:一,效果图。二,工程图。三,代码。ViewController.m#import"ViewController.h"@interfaceViewController()@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.//初始化背景图[selfaddBackGroundView];
  • 标签:笔记 iOS 代码

一,效果图。

二,工程图。

三,代码。

ViewController.m

class="cnblogs_code_copy" style="font-size: 18px">复制代码
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //初始化背景图
    [self addBackGroundView];
    
}
#pragma -mark -functions
//初始化背景图
-(void)addBackGroundView
{
    //4个连着的button
    for (int i=0; i<4; i++) {
        
        UIButton *chooseButton=[UIButton buttonWithType:UIButtonTypeCustom];
        chooseButton.frame=CGRectMake(0+80*i,50, 80, 38);
        chooseButton.tag=i;
        if (i==0) {
            [chooseButton setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_getno1"] forState:UIControlStateNormal];
        }else if (i==1){
            [chooseButton setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_getyes1"] forState:UIControlStateNormal];
        }else if (i==2){
            [chooseButton setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_workno1"] forState:UIControlStateNormal];
        }else if (i==3){
            [chooseButton setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_workyes1"] forState:UIControlStateNormal];
        }
        [chooseButton setBackgroundColor:[UIColor clearColor]];
        [chooseButton addTarget:self action:@selector(doClickChooseButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:chooseButton];
        
    }

}
#pragma -mark -doClickActions
//4个按钮
-(void)doClickChooseButton:(UIButton *)btn
{
    NSLog(@"---doClickChooseButton--");
    if (btn.tag==0) {
        if (btn.selected==NO) {
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_getno2"] forState:UIControlStateNormal];
            btn.selected=YES;
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_getno1"] forState:UIControlStateNormal];
            btn.selected=NO;
        }
        
    }else if (btn.tag==1){
        if (btn.selected==NO) {
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_getyes2"] forState:UIControlStateNormal];
            btn.selected=YES;
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_getyes1"] forState:UIControlStateNormal];
            btn.selected=NO;
        }
        
    }else if (btn.tag==2){
        if (btn.selected==NO) {
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_workno2"] forState:UIControlStateNormal];
            btn.selected=YES;
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_workno1"] forState:UIControlStateNormal];
            btn.selected=NO;
        }
        
    }else if (btn.tag==3){
        if (btn.selected==NO) {
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_workyes2"] forState:UIControlStateNormal];
            btn.selected=YES;
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:@"shoplist_tab_workyes1"] forState:UIControlStateNormal];
            btn.selected=NO;
        }
        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
复制代码
发表评论
用户名: 匿名