UIPickerView的簡單使用

從寫了營業(yè)時間的界面,用到了小時键俱、分鐘
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>
//遵循協(xié)議
@property (nonatomic,strong)UIPickerView * pickerView;//自定義pickerview
@property (nonatomic,strong)NSArray *hourArray;//保存的小時
@property (nonatomic,strong)NSArray *minuteArray;//保存要展示的數(shù)字
//上班
@property(nonatomic,strong)UILabel *openLabel;
//下班
@property(nonatomic,strong)UILabel *closeLabel;
//陰影模板
@property (nonatomic,strong)UIView *shadowView;
//內(nèi)容模板
@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;
//區(qū)分上下班顯示
@property(nonatomic,assign)NSInteger buttonTag;
@end

@implementation RBSetWorkTimeVc

- (void)viewDidLoad {
    [super viewDidLoad];
    //設置導航欄
    [self initNavigation];
    //初始化視圖
    [self initView];
}

//設置導航欄
-(void)initNavigation{
    self.navigationItem.title = @"營業(yè)時間";
    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;
    //創(chuàng)建 pickerView
    [self initPickerView];
    //上下班時間默認值方法
    [self setCutWithString:self.openTime];
}

//下班點擊
-(void)closeViewClick{
    self.buttonTag = 20001;
    //創(chuàng)建 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];
    }];
}

//創(chuàng)建 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];
    
    //需要展示的數(shù)據(jù)以數(shù)組的形式保存
    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{
    //參數(shù)
    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;
}

//指定每個表盤上有幾行數(shù)據(jù)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger result = 0;
    switch (component) {
        case 0:
            result = self.hourArray.count;//根據(jù)數(shù)組的元素個數(shù)返回幾行數(shù)據(jù)
            break;
        case 1:
            result = self.minuteArray.count;
            break;
            
        default:
            break;
    }
    
    return result;
}

//指定每行如何展示數(shù)據(jù)(此處和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


最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末兰绣,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子编振,更是在濱河造成了極大的恐慌缀辩,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異雌澄,居然都是意外死亡斋泄,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門镐牺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炫掐,“玉大人,你說我怎么就攤上這事睬涧∧嘉福” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵畦浓,是天一觀的道長痹束。 經(jīng)常有香客問我,道長讶请,這世上最難降的妖魔是什么祷嘶? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮夺溢,結果婚禮上论巍,老公的妹妹穿的比我還像新娘。我一直安慰自己风响,他們只是感情好嘉汰,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著状勤,像睡著了一般鞋怀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上持搜,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天密似,我揣著相機與錄音,去河邊找鬼朵诫。 笑死辛友,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的剪返。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼邓梅,長吁一口氣:“原來是場噩夢啊……” “哼脱盲!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起日缨,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤钱反,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體面哥,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡哎壳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了尚卫。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片归榕。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖吱涉,靈堂內(nèi)的尸體忽然破棺而出刹泄,到底是詐尸還是另有隱情,我是刑警寧澤怎爵,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布特石,位于F島的核電站,受9級特大地震影響鳖链,放射性物質(zhì)發(fā)生泄漏姆蘸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一芙委、第九天 我趴在偏房一處隱蔽的房頂上張望逞敷。 院中可真熱鬧,春花似錦题山、人聲如沸兰粉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽玖姑。三九已至,卻和暖如春慨菱,著一層夾襖步出監(jiān)牢的瞬間焰络,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工符喝, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留闪彼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓协饲,卻偏偏與公主長得像畏腕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子茉稠,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容