UIPickerView的简单使用

从写了营业时间的界面,用到了小时、分钟
Simulator Screen Shot 2017年3月21日 下午9.59.50.png
//
//  RBSetWorkTimeVc.m
//  HuiHomeRestaurant
//
//  Created by 王一 on 2017/3/21.
//  Copyright © 2017年 wangyi. All rights reserved.
//

#import "RBSetWorkTimeVc.h"
#import "NSString+RBExtension.h"

@interface RBSetWorkTimeVc ()<UIPickerViewDataSource,UIPickerViewDelegate>
//遵循协议
@property (nonatomic,strong)UIPickerView * pickerView;//自定义pickerview
@property (nonatomic,strong)NSArray *hourArray;//保存的小时
@property (nonatomic,strong)NSArray *minuteArray;//保存要展示的数字
//上班
@property(nonatomic,strong)UILabel *openLabel;
//下班
@property(nonatomic,strong)UILabel *closeLabel;
//阴影模板
@property (nonatomic,strong)UIView *shadowView;
//内容模板
@property (nonatomic,strong)UIView *contentView;
//分享的View高度
@property(nonatomic,assign)CGFloat ShareViewHeight;
//上班时间View
@property(nonatomic,strong) UIView *openView;
//下班时间View
@property(nonatomic,strong) UIView *closeView;
//UIPickerView 小时字符串
@property(nonatomic,copy)NSString *openStr;
//UIPickerView 分钟字符串
@property(nonatomic,copy)NSString *closeStr;
//区分上下班显示
@property(nonatomic,assign)NSInteger buttonTag;
@end

@implementation RBSetWorkTimeVc

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏
    [self initNavigation];
    //初始化视图
    [self initView];
}

//设置导航栏
-(void)initNavigation{
    self.navigationItem.title = @"营业时间";
    self.view.backgroundColor = WYGlobalBg;
}

//初始化视图
-(void)initView{
    //上班时间View
    self.openView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 + 64, WYScreenW, 50)];
    self.openView.backgroundColor = [UIColor whiteColor];
    self.openView.userInteractionEnabled=YES;
    UITapGestureRecognizer *openViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openViewClick)];
    [self.openView addGestureRecognizer:openViewTap];
    [self.view addSubview:self.openView];
    //文字
    self.openLabel = [[UILabel alloc]init];
    [self setLabel:self.openLabel WithFont:15];
    self.openLabel.text = [NSString stringWithFormat:@"上班时间: %@",self.openTime];
    [self.openView addSubview:self.openLabel];
    [self.openLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.openView.mas_left).offset(10);
        make.centerY.equalTo(self.openView.mas_centerY);
    }];
    
    //下班时间View
    self.closeView = [[UIView alloc]initWithFrame:CGRectMake(0, 50 + 64 + 1, WYScreenW, 50)];
    self.closeView.backgroundColor = [UIColor whiteColor];
    self.closeView.userInteractionEnabled=YES;
    UITapGestureRecognizer *closeViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeViewClick)];
    [self.closeView addGestureRecognizer:closeViewTap];
    [self.view addSubview:self.closeView];
    //文字
    self.closeLabel = [[UILabel alloc]init];
    [self setLabel:self.closeLabel WithFont:15];
    self.closeLabel.text = [NSString stringWithFormat:@"下班时间: %@",self.closeTime];
    [self.closeView addSubview:self.closeLabel];
    [self.closeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.closeView.mas_left).offset(10);
        make.centerY.equalTo(self.closeView.mas_centerY);
    }];
    
    //提交按钮
    BButton *submitBtn = [[BButton alloc]initWithFrame:CGRectMake(10, WYScreenH - 120, WYScreenW - 20, 40) color:WYRGBColor(255, 45, 80) style:BButtonStyleBootstrapV3];
    [submitBtn setTitle:@"提  交" forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(dismissShareViewFinish) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.view addSubview:submitBtn];
}

-(void)setLabel:(UILabel *)label WithFont:(NSInteger)font{
    label.textColor = WYRGBColor(51, 51, 51);
    label.font = [UIFont systemFontOfSize:font];
}

//上班点击
-(void)openViewClick{
    self.buttonTag = 20000;
    //创建 pickerView
    [self initPickerView];
    //上下班时间默认值方法
    [self setCutWithString:self.openTime];
}

//下班点击
-(void)closeViewClick{
    self.buttonTag = 20001;
    //创建 pickerView
    [self initPickerView];
    //上下班时间默认值方法
    [self setCutWithString:self.closeTime];
}

//上下班时间默认值方法
-(void)setCutWithString:(NSString *)String{
    NSString *frontStr = @"0";
    NSString *backStr = @"0";
    if (![NSString isBlankString:String]) {
        for (int i = 0; i < String.length; i++) {
            NSString *str = [String substringWithRange:NSMakeRange(i, 1)];
            if ([str isEqualToString:@":"]) {
                frontStr = [String substringWithRange:NSMakeRange(0,i)];
                NSInteger j = i + 1;
                backStr = [String substringWithRange:NSMakeRange(j,String.length - j)];
            }
        }
    }
    [self.pickerView selectRow:[frontStr intValue] inComponent:0 animated:NO];
    [self.pickerView selectRow:[backStr intValue] inComponent:1 animated:NO];
    self.openStr = frontStr;
    self.closeStr = backStr;
}

//关闭阴影
-(void)dismissShareView{
    [UIView animateWithDuration:0.3 animations:^{
        self.shadowView.alpha = 0.0;
        self.contentView.frame = CGRectMake(0, WYScreenH, WYScreenW, self.ShareViewHeight);
    } completion:^(BOOL finished) {
        //移除
        UIView *shadowView = [self.shadowView viewWithTag:10000];
        [shadowView removeFromSuperview];
        UIView *contentView = [self.contentView viewWithTag:10001];
        [contentView removeFromSuperview];
    }];
}

//创建 pickerView
-(void)initPickerView{
    //阴影
    self.ShareViewHeight = 250;
    self.shadowView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.shadowView.backgroundColor = [UIColor blackColor];
    self.shadowView.tag = 10000;
    self.shadowView.alpha = 0.0;
    [self.shadowView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissShareView)]];
    [self.view addSubview:self.shadowView];
    
    //容器View
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, WYScreenH, WYScreenW, self.ShareViewHeight)];
    self.contentView.tag = 10001;
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.contentView];
    
    //需要展示的数据以数组的形式保存
    self.hourArray = @[@"00",@"01", @"02", @"03", @"04", @"05", @"06", @"07",
                    @"08", @"09", @"10", @"11", @"12", @"13", @"14",
                    @"15", @"16", @"17", @"18", @"19", @"20", @"21",
                    @"22", @"23"];
    self.minuteArray = @[@"00", @"01", @"02", @"03", @"04", @"05", @"06",@"07",
                    @"08", @"09", @"10", @"11", @"12", @"13", @"14", @"15",
                    @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23",
                    @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31",
                    @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39",
                    @"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47",
                    @"48", @"49", @"50", @"51", @"52", @"53", @"54", @"55",
                    @"56", @"57", @"58", @"59"];
    // 初始化pickerView
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, WYScreenW, 200)];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.contentView addSubview:self.pickerView];
    
    //取消按钮
    BButton *cancelBtn = [[BButton alloc]initWithFrame:CGRectMake(20, 10, 60, 30) type:BButtonTypeDefault style:BButtonStyleBootstrapV3];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(dismissShareView) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:cancelBtn];
    
    //确定按钮
    BButton *finishBtn = [[BButton alloc]initWithFrame:CGRectMake(WYScreenW - 80, 10, 60, 30)  type:BButtonTypeDefault style:BButtonStyleBootstrapV3];
    [finishBtn setTitle:@"确定" forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(dismissShareView) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:finishBtn];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.shadowView.alpha = 0.3;
        self.contentView.frame = CGRectMake(0, WYScreenH - self.ShareViewHeight, WYScreenW, self.ShareViewHeight);
    }];
}

//提交按钮
-(void)dismissShareViewFinish{
    //参数
    NSString *store_id = [NSString stringWithFormat:@"%@",[UserDefaults getUserInfo][@"store_id"]];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"app"] = @"store";
    params[@"func"] = @"updateOpeationTime";
    params[@"store_id"] = store_id;
    params[@"order_time_start"] = self.openTime;
    params[@"order_time_end"] = self.closeTime;
    //请求
    [RB_NetAPIManager RB_PostRequestWithparams:params success:^(id json) {
        if ([json[@"status"] intValue] == 1) {
            [SVProgressHUD setMinimumDismissTimeInterval:1.0];
            [SVProgressHUD showSuccessWithStatus:json[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIPickerView代理方法
//指定pickerview有几个表盘
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger result = 0;
    switch (component) {
        case 0:
            result = self.hourArray.count;//根据数组的元素个数返回几行数据
            break;
        case 1:
            result = self.minuteArray.count;
            break;
            
        default:
            break;
    }
    
    return result;
}

//指定每行如何展示数据(此处和tableview类似)
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString * title = nil;
    
    switch (component) {
        case 0:{
            title = self.hourArray[row];
        }break;
            
        case 1:{
            title = self.minuteArray[row];
        }break;
            
        default:
            break;
    }
    return title;
}

//选择了那一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.openStr = self.hourArray[row];
    }else{
        self.closeStr = self.minuteArray[row];
    }
    
    if (self.buttonTag == 20000) {
        self.openTime = [NSString stringWithFormat:@"%@:%@",self.openStr,self.closeStr];
        self.openLabel.text = [NSString stringWithFormat:@"上班时间: %@",self.openTime];
    }
    
    if (self.buttonTag == 20001) {
        self.closeTime = [NSString stringWithFormat:@"%@:%@",self.openStr,self.closeStr];
        self.closeLabel.text = [NSString stringWithFormat:@"下班时间: %@",self.closeTime];
    }
}

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

@end


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容