iOS 自定义只有年月的DatePikerView
头文件:
@interface YearMonthPikerView : UIView @property (nonatomic,copy) void(^cancelBlock)(); @property (nonatomic,copy) void(^sureBlock)(NSString*,NSString*); @end
实现文件:
#import "YearMonthPikerView.h" static const int loop = ; @interface YearMonthPikerView()<UIPickerViewDelegate,UIPickerViewDataSource> @property (weak, nonatomic) IBOutlet UIButton *cancelBtn; @property (weak, nonatomic) IBOutlet UIButton *sureBtn; @property (weak, nonatomic) IBOutlet UIPickerView *ymPikerView; @property (nonatomic,strong) NSArray *monthsArr; //月份的数组 @property (nonatomic,strong) NSArray *yearsArr; //年份的数组 @property (nonatomic,strong) NSDateFormatter *formatter; @property (nonatomic,strong) NSString *currentYear; @property (nonatomic,strong) NSString *currentMonth; @property (nonatomic,assign) BOOL isCurrentYear; @property (nonatomic,strong) NSString *selectYear; @property (nonatomic,strong) NSString *selectMonth; @end @implementation YearMonthPikerView - (void)awakeFromNib{
[super awakeFromNib]; self.ymPikerView.delegate = self;
self.ymPikerView.dataSource = self; //初始化
self.monthsArr = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]; [self.formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] ];
NSDate *currentDate = [NSDate date]; [self.formatter setDateFormat:@"MM"];
self.currentMonth = [self.formatter stringFromDate:currentDate];
self.selectMonth = self.monthsArr[(self.currentMonth.integerValue-)]; [self.formatter setDateFormat:@"yyyy"];
self.currentYear = [self.formatter stringFromDate:currentDate];
self.selectYear = self.currentYear; NSMutableArray <NSString*>*yearsArr = [NSMutableArray array];
[yearsArr addObject:self.currentYear]; int nowYear = [self.currentYear intValue];
for (int i=; i<loop; i++) {
nowYear = nowYear - ;
NSString *nowYearStr = [NSString stringWithFormat:@"%d",nowYear];
[yearsArr insertObject:nowYearStr atIndex:];
}
self.yearsArr = yearsArr;
self.isCurrentYear = YES;
[self.ymPikerView selectRow:(self.yearsArr.count-) inComponent: animated:YES];
[self.ymPikerView selectRow:(self.currentMonth.intValue-) inComponent: animated:YES]; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2.0;
} - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==) {
return self.yearsArr.count;
}else if (component==){
if (self.isCurrentYear) {
return self.currentMonth.integerValue;
}else{
return self.monthsArr.count;
}
}else{
return ;
}
} - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component==) {
NSString *year = self.yearsArr[row];
year = [year stringByAppendingString:@"年"];
return year;
}else if (component==){
NSString *month = self.monthsArr[row];
month = [month stringByAppendingString:@"月"];
return month;
}else{
return nil;
}
} - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component== && row==self.yearsArr.count-) {
self.isCurrentYear = YES;
[self.ymPikerView reloadComponent:]; self.selectYear = self.yearsArr[row];
}else if (component==){
self.isCurrentYear = NO;
[self.ymPikerView reloadComponent:]; self.selectYear = self.yearsArr[row];
}else if (component==){
self.selectMonth = self.monthsArr[row];
}
} - (IBAction)cancelBtnClick:(UIButton *)sender {
if (self.cancelBlock) {
self.cancelBlock();
}
} - (IBAction)sureBtnClick:(UIButton *)sender {
if (self.sureBlock) {
self.sureBlock(self.selectYear, self.selectMonth);
}
} #pragma mark - 懒加载
- (NSDateFormatter*)formatter{
if (_formatter==nil) {
_formatter = [[NSDateFormatter alloc]init];
}
return _formatter;
} @end
效果如下图:
Demo地址如下:https://github.com/LuPing-Kuang/iOS-YearMonthPickerView
iOS 自定义只有年月的DatePikerView的更多相关文章
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- 如何实现 iOS 自定义状态栏
给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...
- iOS自定义组与组之间的距离以及视图
iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
- iOS 自定义转场动画
代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...
- iOS 自定义转场动画浅谈
代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...
- iOS自定义转场动画实战讲解
iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...
- WPF自定义选择年月控件详解
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- IOS自定义表格UITableViewCell
在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了. 1.在storyboard中 给一个ViewController的tabi ...
随机推荐
- Dubbo与Zookeeper 简介
转自http://blog.csdn.net/congcong68/article/details/41113239 首先说一下Dubbo解决什么问题: (1)当服务越来越多时,服务Url配置管理变得 ...
- JS高级学习笔记(10) 之 js 时怎么解析HTML标签的
DOM 节点类型 浏览器渲染过程 浏览器是怎么把HTML标签语言和JavaScript联系在一起的,这就是我们常说的DOM. 浏览器中的DOM解析器把HTML翻译成对象(object),然后JavaS ...
- oo第四单元及课程总结
一.第四单元作业总结 第四单元有两次作业,第十三次作业是实现一个UML类图解析器,可以通过输入一些查询指令来查询一些类图的信息.程序的主干部分已经提供,我们的任务就是实现给出的接口,过程并不繁琐.第十 ...
- 51nod 1013:3的幂的和 快速幂
1013 3的幂的和 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 求:3^0 + 3^1 +...+ 3^(N) mod 1000000007 ...
- mnist lenet caffe 测试
# -*- coding: utf-8 -*-import sysimport numpy as npimport structfrom PIL import Imageprint "hel ...
- 不使用.h .lib文件使用DLL内的函数
#include <windows.h> typedef int (*Func)(const char *fmt, ...); //这里声明一个函数指针,typedef 关键字是必须的,好 ...
- 最短路问题-- Dijkstra Choose the best route
Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...
- POJ 2528 Mayor‘s poster 线段树+离散化
给一块最大为10^8单位宽的墙面,贴poster,每个poster都会给出数据 a,b,表示该poster将从第a单位占据到b单位,新贴的poster会覆盖旧的,最多有10^4张poster,求最后贴 ...
- Freemarker的一点延生
1.freemarker一般不是单独使用,他可以和activeMQ互相结合,来完成功能的. 使用它有 几点好处, 首先就是提高程序效率,一般情况我们的页面都是jsp,而jsp实际上是servlet,在 ...
- Leetcode -- 两数之和Ⅰ
1. 两数之和 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 示例:给定 nums = [2, 7, 11, 15 ...