您的位置:1010cc时时彩经典版 > 操作系统 > 城市选择三级联动选择器,ios自定义选择器Acti

城市选择三级联动选择器,ios自定义选择器Acti

发布时间:2019-08-18 11:31编辑:操作系统浏览(142)

    ios自定义采用器ActionSheetPicker立异版,iospicker

            ios自带的UIDataPicker和UIDatePicker最大的病症就是从未带明确和撤销这三个按键,而ActionSheetPicker是以上四个采取器的开源封装。不过这一个东东也许有个别小意思,正是没有汉化选取器的剧情。

    村生泊长的ActionSheetPicker运营起来是这些样子的:

    图片 1

     

    精益求精后是那以下那一个样子的

    图片 2

     

    点击这里下载,有标题可加扣扣359709421评论。

     

    补充三个破绽的修补,在ios7以上版本中,选拔器数据部份是晶莹的,很不佳看,

    把AbstractActionSheetPicker.m的showActionSheetPicker方法加上一行

    masterView.backgroundColor=[UIColor whiteColor];

    即能够解决此主题素材。

     

    Java代码 图片 3 图片 4

    - (void)showActionSheetPicker {  

        UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)];      

        UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:self.title];  

        [pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];  

        [masterView addSubview:pickerToolbar];  

        self.pickerView = [self configuredPickerView];  

        NSAssert(_pickerView != NULL, @"Picker view failed to instantiate, perhaps you have invalid component data.");  

        [masterView addSubview:_pickerView];  

        masterView.backgroundColor=[UIColor whiteColor];  

        [self presentPickerForView:masterView];  

    }  

    - (void)showActionSheetPicker {
        UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)];    
        UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:self.title];
        [pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
        [masterView addSubview:pickerToolbar];
        self.pickerView = [self configuredPickerView];
        NSAssert(_pickerView != NULL, @"Picker view failed to instantiate, perhaps you have invalid component data.");
        [masterView addSubview:_pickerView];
        masterView.backgroundColor=[UIColor whiteColor];
        [self presentPickerForView:masterView];
    }
    

     

     笔者的网店,有劳各位游历浏览 

    • 图片 5
    • 大小: 256.1 KB
    • 图片 6
    • 大小: 221.6 KB
    • TimCinel-ActionSheetPicker-4d01f72.zip

            ios自带的UIDataPicker和UIDatePicker最大的病症正是从未带分明和注销那三个开关,而ActionSheetPicker是以上三个选择器的开源封装。然而这些东东也某些小难题,便是未有汉化选取器的剧情。

    iOS学习——UIPickerView的兑现时间采用器,iosuipickerview

      这两天项目上必要动用三个选取器,选用器中的内容唯有年和月,而在iOS系统自带的日子选择器UIDatePicker中却唯有四个挑选如下,分别是时刻(时分秒)、日期(年月日)、日期 时间(年月日时分)以及倒计时。当中并从未大家所急需的只展示时间的选择器,在网络找了过多相关的材质,不过认为都写得过于辛劳。由此,为了满意项目须要,自个儿用UIPickerView写了三个只展示时间的选择器分界面,同期还足以操纵我们的显示的小不点儿时间。当然,假如要调控别的剧情也都以能够的,无非正是在多少管理上多一些管理和垄断(monopoly)。

    typedef NS_ENUM(NSInteger, UIDatePickerMode) {
        UIDatePickerModeTime,           // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
        UIDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
        UIDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
        UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
    } __TVOS_PROHIBITED;
    

    #import "ViewController.h"

    //项目

    本来的ActionSheetPicker运维起来是以此样子的:

    一 全体方案 

      在一切完毕中分成五个部分,首先是用二个基类来布局大家接纳器的总体布局,包含大家的选取器的标题,撤销、明确开关,蒙层等大框架的布局,然后是子类在基类的底蕴上加多UIPickerView来落到实处选拔器的基本成效以及数据加载和显示。首先,大家来看一下完整的叁个职能,点击某个设定的控件,然后弹出下图所示的一个采用器,选取器的选项根本就是显年月的音讯:

      在上某个说了,基类布局器重是对完全的架构举行布局,大家先看下有怎么着内容,富含了背景蒙层视图、弹出视图(包罗标题行(又含有撤除按键、鲜明开关和标题)、分水岭和选拔器),在子类中会实行几个完好无缺的布局,在 - (void)initUI 方法中张开布局。

    //
    //  BaseView.h
    
    #import <UIKit/UIKit.h>
    
    #define kDatePicHeight 200  //选择器的高度
    #define kTopViewHeight 44   //取消 标题 确定 行高度
    
    #define SCREEN_BOUNDS [UIScreen mainScreen].bounds
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    @interface BaseView : UIView
    // 背景蒙层视图
    @property (nonatomic, strong) UIView *backgroundView;
    // 弹出视图
    @property (nonatomic, strong) UIView *alertView;
    // 标题行顶部视图
    @property (nonatomic, strong) UIView *topView;
    // 左边取消按钮
    @property (nonatomic, strong) UIButton *leftBtn;
    // 右边确定按钮
    @property (nonatomic, strong) UIButton *rightBtn;
    // 中间标题
    @property (nonatomic, strong) UILabel *titleLabel;
    // 分割线视图
    @property (nonatomic, strong) UIView *lineView;
    
    /** 初始化子视图 ,整体布局*/
    - (void)initUI;
    
    //以下三种方法在基类中的实现都是空白的,具体的效果在子类中重写
    /** 点击背景遮罩图层事件 */
    - (void)didTapBackgroundView:(UITapGestureRecognizer *)sender;
    /** 取消按钮的点击事件 */
    - (void)clickLeftBtn;
    /** 确定按钮的点击事件 */
    - (void)clickRightBtn;
    
    @end
    

      具体的.m文件的达成代码如下,进行折叠了,供给的能够一直拷贝,在后面大家再 举办具体深入分析每一步的布局和安装。

    图片 7 1 // 2 // BaseView.m 3 // CJMobile 4 // 5 // Created by mukekeheart on 2017/12/12. 6 // Copyright © 2017年 刚果河股票(stock). All rights reserved. 7 // 8 9 #import "BaseView.h" 10 11 @implementation BaseView 12 13 - (void)initUI { 14 self.frame = SCREEN_BOUNDS; 15 // 背景遮罩图层 16 [self addSubview:self.backgroundView]; 17 // 弹出视图 18 [self addSubview:self.alertView]; 19 // 安装弹出视图子视图 20 // 加多最上端标题栏 21 [self.alertView addSubview:self.topView]; 22 // 增多左侧撤消按键 23 [self.topView addSubview:self.leftBtn]; 24 // 加多侧面显著开关 25 [self.topView addSubview:self.rightBtn]; 26 // 加多中间标题开关 27 [self.topView addSubview:self.titleLabel]; 28 // 增多分界线 29 [self.topView addSubview:self.lineView]; 30 } 31 32 #pragma mark - 背景遮罩图层 33 - (UIView *)backgroundView { 34 if (!_backgroundView) { 35 _backgroundView = [[UIView alloc]initWithFrame:SCREEN_BOUNDS]; 36 _backgroundView.backgroundColor = [UIColor blackColor] ; 37 _backgroundView.alpha = 0.3f ; 38 _backgroundView.userInteractionEnabled = YES; 39 UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)]; 40 [_backgroundView addGestureRecognizer:myTap]; 41 } 42 return _backgroundView; 43 } 44 45 #pragma mark - 弹出视图 46 - (UIView *)alertView { 47 if (!_alertView) { 48 _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - kTopViewHeight - kDatePicHeight, SCREEN_WIDTH, kTopViewHeight kDatePicHeight)]; 49 _alertView.backgroundColor = [UIColor whiteColor]; 50 } 51 return _alertView; 52 } 53 54 #pragma mark - 顶上部分标题栏视图 55 - (UIView *)topView { 56 if (!_topView) { 57 _topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kTopViewHeight 0.5)]; 58 _topView.backgroundColor = [UIColor whiteColor]; 59 } 60 return _topView; 61 } 62 63 #pragma mark - 左侧撤除开关 64 - (UIButton *)leftBtn { 65 if (!_leftBtn) { 66 _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 67 _leftBtn.frame = CGRectMake(5, 8, 60, 28); 68 _leftBtn.backgroundColor = [UIColor clearColor]; 69 _leftBtn.layer.masksToBounds = YES; 70 _leftBtn.titleLabel.font = [UIFont systemFontOfSize:17.0f]; 71 [_leftBtn setTitleColor:kGrayFontColor forState:UIControlStateNormal]; 72 [_leftBtn setTitle:@"取消" forState:UIControlStateNormal]; 73 [_leftBtn addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside]; 74 } 75 return _leftBtn; 76 } 77 78 #pragma mark - 侧边鲜明按键 79 - (UIButton *)rightBtn { 80 if (!_rightBtn) { 81 _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 82 _rightBtn.frame = CGRectMake(SCREEN_WIDTH - 65, 8, 60, 28); 83 _rightBtn.backgroundColor = [UIColor clearColor]; 84 _rightBtn.layer.masksToBounds = YES; 85 _rightBtn.titleLabel.font = [UIFont systemFontOfSize:17.0f]; 86 [_rightBtn setTitleColor:kBlueFontColor forState:UIControlStateNormal]; 87 [_rightBtn setTitle:@"确定" forState:UIControlStateNormal]; 88 [_rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside]; 89 } 90 return _rightBtn; 91 } 92 93 #pragma mark - 中间题目按键 94 - (UILabel *)titleLabel { 95 if (!_titleLabel) { 96 _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 0, SCREEN_WIDTH - 130, kTopViewHeight)]; 97 _titleLabel.backgroundColor = [UIColor clearColor]; 98 _titleLabel.font = [UIFont systemFontOfSize:17.0f]; 99 _titleLabel.textColor = kBlackFontColor; 100 _titleLabel.textAlignment = NSTextAlignmentCenter; 101 } 102 return _titleLabel; 103 } 104 105 #pragma mark - 分割线 106 - (UIView *)lineView { 107 if (!_lineView) { 108 _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, kTopViewHeight, SCREEN_WIDTH, 0.5)]; 109 _lineView.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1.0]; 110 [self.alertView addSubview:_lineView]; 111 } 112 return _lineView; 113 } 114 115 #pragma mark - 点击背景遮罩图层事件 116 - (void)didTapBackgroundView:(UITapGestureRecognizer *)sender { 117 118 } 119 120 #pragma mark - 撤销开关的点击事件 121 - (void)clickLeftBtn { 122 123 } 124 125 #pragma mark - 分明按键的点击事件 126 - (void)clickRightBtn { 127 128 } 129 130 @end BaseView.m

      在BaseView.m中关键是对整体框架进行布局,大家的控件的岗位都以透过相对地点张开布局的,所以须要修改的在话能够直接在相应的职位上进展改换,然后在BaseView.h中的注释大家说过了,点击背景遮罩图层和注销、显著按键的点击事件达成效果与利益在基类中都以赤手的,具体成效在子类中张开重写来调节。而对于弹出视图中的标题行(包蕴撤消按键、鲜明按键和标题)、分界线和接纳器的切切实实布局在那边就不开始展览拓展了,很粗大略的一些,大家自行看一下代码就OK了。

      上面首要提七个难点:一个是欧洲经济共同体布局的不二法门 - (void)initUI 的完结。这里大家根本要小心的丰盛的层系,哪个人是什么人的子视图,必须要分别清楚。

    - (void)initUI {
        self.frame = SCREEN_BOUNDS;
        // 背景遮罩图层
        [self addSubview:self.backgroundView];
        // 弹出视图
        [self addSubview:self.alertView];
        // 设置弹出视图子视图
        // 添加顶部标题栏
        [self.alertView addSubview:self.topView];
        // 添加左边取消按钮
        [self.topView addSubview:self.leftBtn];
        // 添加右边确定按钮
        [self.topView addSubview:self.rightBtn];
        // 添加中间标题按钮
        [self.topView addSubview:self.titleLabel];
        // 添加分割线
        [self.topView addSubview:self.lineView];
    }
    

      二是大家的背景蒙层和弹出视图我们能够通过代码看到蒙层遮罩背景的布局是全部荧屏,那么大家为何不直接在蒙层上增添弹出式图呢?假使一贯在蒙层上增多弹出式图作为子视图的话,大家的布局相对会轻易非常多,这里提到到一点便是子视图的光滑度是和父视图保持一致的,假诺向来将弹出视图加载到蒙层遮罩视图上,会招致弹出视图的光滑度也为0.3,所以弹出视图无法间接加在蒙层遮罩视图上,而是必要加在当前分界面上。

    - (UIView *)backgroundView {
        if (!_backgroundView) {
            _backgroundView = [[UIView alloc]initWithFrame:SCREEN_BOUNDS];
            _backgroundView.backgroundColor = [UIColor blackColor] ;
            _backgroundView.alpha = 0.3f ;
            _backgroundView.userInteractionEnabled = YES;
            UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
            [_backgroundView addGestureRecognizer:myTap];
        }
        return _backgroundView;
    }
    
    
        // 背景遮罩图层
        [self addSubview:self.backgroundView];
        // 弹出视图
        [self addSubview:self.alertView];
    

    图片 8

    import UIKit

    ios自带的UIDataPicker和UIDatePicker最大的病魔正是从未带分明和收回那三个按键,而ActionSheetPick...

    图片 9

    三 子类选用器实现

      首先是我们的子类向外暴光的办法独有叁个类情势,该办法首若是让使用者提供采纳器的标题、最小日期、日期选取成功后的操作等骨干消息,方便大家对选拔器的多少和操作实行安装。对外揭发类方法也是制止使用者在应用时索要成立对象,比较费心,也防止有个别不须要的难题。

    //
    //  CJYearMonthSelectedView.h
    
    #import <UIKit/UIKit.h>
    #import "BaseView.h"
    
    //日期选择完成之后的操作
    typedef void(^BRDateResultBlock)(NSString *selectValue);
    
    @interface CJYearMonthSelectedView : BaseView
    
    //对外开放的类方法
      (void)showDatePickerWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock;
    
    @end
    

       关于现实的子类的贯彻,依然先把具有代码都贴上来,有一点点多,所以折叠一下,前面临内部一部分要点进行列出说澳优(Ausnutria Hyproca)(Dumex)下。还会有裁撤、分明按键的点击事件也都在此处开始展览支配和完毕,大家依据自身的内需实行那是就足以了,一般是在点击显明按键的时候调用大家的B福睿斯DateResultBlock,实现日期采纳成功的操作。个中裁撤按键就径直没有操作,dismiss当前分界面,并小心要拓展dealloc,创造的视图要化解,幸免内存败露。蒙层背景点击事件看供给,有的必要和收回同样的职能,有的或许就无效果,本人加上就可以。

    图片 10 1 // CJYearMonthSelectedView.m 2 3 #import "CJYearMonthSelectedView.h" 4 5 @interface CJYearMonthSelectedView () <UIPickerViewDelegate,UIPickerViewDataSource> 6 @property (strong, nonatomic) UIPickerView *picker; //选择器 7 @property (copy, nonatomic) NSString *title; 8 @property (copy, nonatomic) NSString *minDateStr; 9 @property (assign, nonatomic) BRDateResultBlock resultBlock; 10 @property (copy, nonatomic) NSString *selectValue; //选用的值 11 @property (strong, nonatomic) NSMutableArray<NSString *> *data; 12 13 @end 14 15 @implementation CJYearMonthSelectedView 16 17 (void)showDatePickerWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock{ 18 19 CJYearMonthSelectedView *datePicker = [[CJYearMonthSelectedView alloc] initWithTitle:title minDateStr:minDateStr resultBlock:resultBlock]; 20 [datePicker showWithAnimation:YES]; 21 } 22 23 //发轫化方法 24 - (instancetype)initWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRDateResultBlock)resultBlock{ 25 if (self = [super init]) { 26 _title = title; 27 _minDateStr = minDateStr; 28 _resultBlock = resultBlock; 29 30 [self initUI]; 31 } 32 33 return self; 34 } 35 36 //UI布局,重要正是在弹出视图上增多采用器 37 - (void)initUI{ 38 [super initUI]; 39 self.titleLabel.text = _title; 40 // 增加时间接选举择器 41 [self.alertView addSubview:self.picker]; 42 } 43 44 //选用器的早先化和布局 45 - (UIPickerView *)picker{ 46 if (!_picker) { 47 _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kTopViewHeight 0.5, SCREEN_WIDTH, kDatePicHeight)]; 48 // _picker.backgroundColor = [UIColor whiteColor]; 49 _picker.showsSelectionIndicator = YES; 50 //设置代理 51 _picker.delegate =self; 52 _picker.dataSource =self; 53 } 54 return _picker; 55 } 56 57 //选用器数据的加载,从设定的细小日期到当后一个月 58 - (NSMutableArray<NSString *> *)data{ 59 if (!_data) { 60 _data = [[NSMutableArray alloc] init]; 61 NSDate *currentDate = [NSDate date]; 62 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 63 [formatter setDateFormat:@"yyyy-MM"]; 64 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 65 NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init]; 66 NSString *dateStr = [formatter stringFromDate:currentDate]; 67 NSInteger lastIndex = 0; 68 NSDate *newdate; 69 //循环获取可选月份,从此时此刻月份到最小月份 70 while (!([dateStr compare:self.minDateStr] == NSOrderedAscending)) { 71 [_data addObject:dateStr]; 72 lastIndex--; 73 //获取此前多少个月 74 [lastMonthComps setMonth:lastIndex]; 75 newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0]; 76 dateStr = [formatter stringFromDate:newdate]; 77 } 78 } 79 return _data; 80 } 81 82 #pragma mark - UIPickerView的数据和布局,和tableview类似 83 //重返多少列 84 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 85 return 1; 86 } 87 88 //重临多少行 89 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 90 return self.data.count; 91 } 92 93 //每一行的数目 94 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 95 return self.data[row]; 96 } 97 98 //选中时的功能 99 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 100 self.selectValue = self.data[row]; 101 } 102 103 //重临中度 104 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ 105 return 35.0f; 106 } 107 108 //再次回到小幅度 109 -(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ 110 return ZYAppWidth; 111 } 112 113 #pragma mark - 背景视图的点击事件 114 - (void)didTapBackgroundView:(UITapGestureRecognizer *)sender { 115 // [self dismissWithAnimation:NO]; 116 } 117 118 #pragma mark - 弹出视图方法 119 - (void)showWithAnimation:(BOOL)animation { 120 //1. 到手当前使用的主窗口 121 UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; 122 [keyWindow addSubview:self]; 123 if (animation) { 124 // 动画前开头地点 125 CGRect rect = self.alertView.frame; 126 rect.origin.y = SCREEN_HEIGHT; 127 self.alertView.frame = rect; 128 // 体现动画 129 [UIView animateWithDuration:0.3 animations:^{ 130 CGRect rect = self.alertView.frame; 131 rect.origin.y -= kDatePicHeight kTopViewHeight; 132 self.alertView.frame = rect; 133 }]; 134 } 135 } 136 137 #pragma mark - 关闭视图方法 138 - (void)dismissWithAnimation:(BOOL)animation { 139 // 安歇动画 140 [UIView animateWithDuration:0.2 animations:^{ 141 CGRect rect = self.alertView.frame; 142 rect.origin.y = kDatePicHeight kTopViewHeight; 143 self.alertView.frame = rect; 144 145 self.backgroundView.alpha = 0; 146 } completion:^(BOOL finished) { 147 [self.leftBtn removeFromSuperview]; 148 [self.rightBtn removeFromSuperview]; 149 [self.titleLabel removeFromSuperview]; 150 [self.lineView removeFromSuperview]; 151 [self.topView removeFromSuperview]; 152 [self.picker removeFromSuperview]; 153 [self.alertView removeFromSuperview]; 154 [self.backgroundView removeFromSuperview]; 155 [self removeFromSuperview]; 156 157 self.leftBtn = nil; 158 self.rightBtn = nil; 159 self.titleLabel = nil; 160 self.lineView = nil; 161 self.topView = nil; 162 self.picker = nil; 163 self.alertView = nil; 164 self.backgroundView = nil; 165 }]; 166 } 167 168 #pragma mark - 撤销开关的点击事件 169 - (void)clickLeftBtn { 170 [self dismissWithAnimation:YES]; 171 } 172 173 #pragma mark - 鲜明开关的点击事件 174 - (void)clickRightBtn { 175 NSLog(@"点击鲜明开关后,施行block回调"); 176 [self dismissWithAnimation:YES]; 177 if (_resultBlock) { 178 _resultBlock(_selectValue); 179 } 180 } 181 182 @end CJYearMonthSelectedView.m

      那之中跟着流程看其实很轻易哈,主要供给声明的一点正是UIPickerView的用法,UIPickerView其实和UITableView很类似,在起初化的时候须要安装其数据代理和视图代理(UIPickerViewDelegate,UIPickerViewDataSource),然后经过那多个代理进内容、行数、列数的支配。

    - (UIPickerView *)picker{
        if (!_picker) {
            _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kTopViewHeight   0.5, SCREEN_WIDTH, kDatePicHeight)];
            _picker.showsSelectionIndicator = YES;
            //设置UIPickerView的代理
            _picker.delegate =self;
            _picker.dataSource =self;
        }
        return _picker;
    }
    
    #pragma mark - UIPickerView
    //返回多少列
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
    }
    
    //返回多少行
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        return self.data.count;
    }
    
    //每一行的数据
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        return self.data[row];
    }
    
    //选中时的效果
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        self.selectValue = self.data[row];
    }
    
    //返回高度
    -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
        return 35.0f;
    }
    
    //返回宽度
    -(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
        return ZYAppWidth;
    }
    

      关于数据的主宰能够依照大家的须求实行设定,行数和列数也是依附大家的须要来进行支配。上边首要便是一下哪些赢得年月那般的数据,重借使用到了NSDateComponents 的直接得到三个月前的音讯,然后经过将NSCalendar将NSDateComponents转化为日期Date,最终将Date转化为大家须要的格式的数额。

    //数据获取
    - (NSMutableArray<NSString *> *)data{
        if (!_data) {
            _data = [[NSMutableArray alloc] init];
            //当前日期时间
            NSDate *currentDate = [NSDate date];
            //设定数据格式为xxxx-mm
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM"];
            //通过日历可以直接获取前几个月的日期,所以这里直接用该类的方法进行循环获取数据
            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
            NSString *dateStr = [formatter stringFromDate:currentDate];
            NSInteger lastIndex = 0;
            NSDate *newdate;
            //循环获取可选月份,从当前月份到最小月份,直接用字符串的比较来判断是否大于设定的最小日期
            while (!([dateStr compare:self.minDateStr] == NSOrderedAscending)) {
                [_data addObject:dateStr];
                lastIndex--;
                //获取之前n个月, setMonth的参数为正则向后,为负则表示之前
                [lastMonthComps setMonth:lastIndex];
                newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
                dateStr = [formatter stringFromDate:newdate];
            }
        }
        return _data;
    }
    

     四 使用情势

      关于自身做的这些在选拔上就非常轻巧了,大家的子类向外就揭发了三个类措施,所以大家再需求弹出接纳器的地点调用该办法就能够了。

    - (void) btnPress:(UIButton *)sender{
        if (sender.tag == 200) { //导出 按钮
            [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2017-10" resultBlock:^(NSString *selectValue) {
                //选择完成后的操作
                NSLog(@"selected month is %@", selectValue);
            }];
        } else {
    
        }
    }
    

       以上就是运用UIPickerView自定义三个年月的选拔器,蕴含最初的的欧洲经济共同体的分界面代码和实际的选用器的创制和布局,以及我们的数量管理。

     

    近日项目上须要动用贰个选择器,选拔器中的内容独有年和月,而在iOS系统自带的日期...

    #define ScreenW [UIScreen mainScreen].bounds.size.width

    import SnapKit

     

    #define ScreenH [UIScreen mainScreen].bounds.size.height

    class ChangeHomeAddressVC: BaseViewController,UIPickerViewDelegate,UIPickerViewDataSource{

    创新后是那以下那一个样子的

    #define BXColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

    var pickerDic:NSDictionary!

    图片 11

    @interface ViewController ()

    var provinceArray:NSArray!

     

    @property (nonatomic, strong) UIDatePicker *datePicker;      // 选择器

    var cityArray:NSArray!

    点击这里下载,卓殊可加扣扣359709421谈谈。

    @property (nonatomic, strong) UIButton *bgView;                // 背景视图

    var townArray:NSArray!

     

    @property (nonatomic, strong) UIView *dateView;              // 日期视图

    var selectedArray:NSArray!

    填补二个败笔的修复,在ios7上述版本中,选拔器数据部份是晶莹剔透的,很不美观,

    @property (nonatomic, strong) NSString *dateString;          // 日期字符

    override func viewDidLoad() {

    把AbstractActionSheetPicker.m的showActionSheetPicker方法加上一行

    @property (nonatomic, strong) NSDateFormatter *dateFormatter;

    super.viewDidLoad()

    masterView.backgroundColor=[UIColor whiteColor];

    @property (nonatomic, strong) NSDate *date;                  // 日期

    self.navigationItem.title = "更换家庭地址"

    即能够消除此主题素材。

    @property (nonatomic, strong) UIButton *sureButton;          // 撤消按键

    //侧面导航键

     

    @property (nonatomic, strong) UIButton *cancelButton;        // 撤除按键

    self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "保存", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ChangeHomeAddressVC.changeAddressByPicker))

    Java代码 图片 12 图片 13图片 14)

    @property (nonatomic, strong) UIButton *btn;

    prepareUI()

    1. - (void)showActionSheetPicker {  
    2.     UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)];      
    3.     UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:self.title];  
    4.     [pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];  
    5.     [masterView addSubview:pickerToolbar];  
    6.     self.pickerView = [self configuredPickerView];  
    7.     NSAssert(_pickerView != NULL, @"Picker view failed to instantiate, perhaps you have invalid component data.");  
    8.     [masterView addSubview:_pickerView];  
    9.     masterView.backgroundColor=[UIColor whiteColor];  
    10.     [self presentPickerForView:masterView];  
    11. }  

      • (void)showActionSheetPicker { UIView masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)];
        UIToolbar
        pickerToolbar = [self createPickerToolbarWithTitle:self.title]; [pickerToolbar setBarStyle:UIBarStyleBlackTranslucent]; [masterView addSubview:pickerToolbar]; self.pickerView = [self configuredPickerView]; NSAssert(_pickerView != NULL, @"Picker view failed to instantiate, perhaps you have invalid component data."); [masterView addSubview:_pickerView]; masterView.backgroundColor=[UIColor whiteColor]; [self presentPickerForView:masterView]; }

    @end

    addPickView()

     

    @implementation ViewController

    getPickerData()

     我的网店,有劳各位游历游历 

    #pragma 懒加载

    }

    • 图片 15
    • 大小: 256.1 KB

    - (UIDatePicker *)datePicker{

    //荧屏点击事件

    if (_datePicker == nil) {

    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

    • 图片 16
    • 大小: 221.6 KB

    _datePicker = [[UIDatePicker alloc] init];

    self.view.endEditing(true)

    _datePicker.frame = CGRectMake(0, 0, ScreenW, 200);

    }

    • TimCinel-ActionSheetPicker-4d01f72.zip

    _datePicker.backgroundColor = [UIColor whiteColor];

    //点击了改变地址的view触发事件

    _datePicker.datePickerMode = UIDatePickerModeDate;

    func changeAddress() {

    [_dateView addSubview:_datePicker];

    print("点击了变动地址的view")

    }

    if cityPickView.frame.origin.y < UIScreen.mainScreen().bounds.height

    return _datePicker;

    • 204{

    }

    print("点击过了")

    - (UIView *)bgView{

    }else {

    if (_bgView == nil) {

    likeToolView.hidden = false

    _bgView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];

    cityPickView.hidden = false

    _bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.25];

    UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {

    [_bgView addTarget:self action:@selector(quXiaoBtn) forControlEvents:UIControlEventTouchUpInside];

    self.likeToolView.transform = CGAffineTransformTranslate(self.likeToolView.transform, 0, -204)

    _bgView.hidden = YES;

    self.cityPickView.transform = CGAffineTransformTranslate(self.cityPickView.transform , 0, -204)

    }

    }, completion: nil)

    return _bgView;

    }

    }

    }

    - (UIView *)dateView{

    //MARK:  -视图

    if (_dateView == nil) {

    func prepareUI() {

    _dateView = [[UIView alloc] init];

    self.view.addSubview(topView)

    }

    topView.addSubview(labelView)

    return _dateView;

    labelView.addSubview(addressLabel)

    }

    labelView.addSubview(cityLabel)

    - (NSString *)dateString{

    labelView.addSubview(districtLabel)

    if (_dateString == nil) {

    topView.addSubview(fengGeLine)

    _dateString = [[NSString alloc] init];

    topView.addSubview(AddressTextField)

    }

    labelView.snp_makeConstraints { (make) in

    return _dateString;

    make.trailing.equalTo(topView.snp_trailing)

    }

    make.leading.equalTo(topView.snp_leading)

    - (NSDateFormatter *)dateFormatter{

    make.top.equalTo(topView.snp_top)

    if (!_dateFormatter) {

    make.height.equalTo(44)

    _dateFormatter = [[NSDateFormatter alloc] init];

    }

    [_dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    cityLabel.snp_makeConstraints { (make) in

    }

    make.centerY.equalTo(addressLabel.snp_centerY)

    return _dateFormatter;

    make.leading.equalTo(addressLabel.snp_trailing).offset(5)

    }

    }

    - (NSDate *)date{

    districtLabel.snp_makeConstraints { (make) in

    if (!_date) {

    make.centerY.equalTo(addressLabel.snp_centerY)

    _date = [[NSDate alloc] init];

    make.leading.equalTo(cityLabel.snp_trailing).offset(5)

    }

    }

    return _date;

    AddressTextField.snp_makeConstraints { (make) in

    }

    make.top.equalTo(fengGeLine.snp_bottom).offset(20)

    - (UIButton *)sureButton{

    make.leading.equalTo(fengGeLine.snp_leading)

    if (_sureButton == nil) {

    make.trailing.equalTo(fengGeLine.snp_trailing)

    _sureButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW - 100*ScreenW/414, 250 - 40*ScreenH/736, 100*ScreenW/414, 40*ScreenH/736)];

    }

    _sureButton.layer.cornerRadius = 5.0;

    fengGeLine.snp_makeConstraints { (make) in

    本文由1010cc时时彩经典版发布于操作系统,转载请注明出处:城市选择三级联动选择器,ios自定义选择器Acti

    关键词:

上一篇:8十大亮点揭秘,10增长难料

下一篇:没有了