最近做项目为了方便项目使用,自定义的空间 写的比较粗糙。欢迎大家批评指正。
以上为在项目中的实际应用
 //  DropDownDatePicker.h
// DropDownDatePickerDemo
//
// Created by HRP on 15/3/25.
// Copyright (c) 2015年 HRP. All rights reserved.
// #import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, DropDownDatePickerMode)
{
DropDownDatePickerModeDate,
DropDownDatePickerModeTime
}; @interface DropDownDatePicker : UIView - (id)initWithFrame:(CGRect)frame dateMode:(DropDownDatePickerMode)mode; @property (nonatomic, copy) void(^dateChangeHandler)(NSDate *); @property (nonatomic, readonly) UILabel *titleLabel;
@property (nonatomic, readonly) UIBarButtonItem *confrimButton; @property (nonatomic,strong ) UIColor *dateTextColor; //Default is Blue
@property (nonatomic,strong ) UIFont *dateTextFont; //Default is 14.0 @property (nonatomic,strong ) NSDate *currentDate; //Default is [NSDate date] @property (nonatomic,retain ) NSDate *minimumDate; //Default is nil
@property (nonatomic,retain ) NSDate *maximumDate; //Default is nil @property (nonatomic,assign ) NSInteger minuteInterval; @end ===== 以下为DropDownDatePicker.m====== //
// DropDownDatePicker.m
// DropDownDatePickerDemo
//
// Created by HRP on 15/3/25.
// Copyright (c) 2015年 HRP. All rights reserved.
// #import "DropDownDatePicker.h" @protocol HRPDropDownDatePickerControllerDelegate <NSObject> @required - (void)datePickerControllerNeedDismiss;
- (void)datePickerControllerDateChanged:(NSDate *)newDate; @end @interface HRPDropDownDatePickerController : UIViewController - (id)initWithDatePickerMode:(DropDownDatePickerMode)mode; @property (nonatomic) UIPopoverController *popover; @property (nonatomic,readonly ) UILabel *titleLable;
@property (nonatomic,readonly ) UIBarButtonItem *confrimButton;
@property (nonatomic,readwrite) NSDate *currentDate; @property (nonatomic,weak)id<HRPDropDownDatePickerControllerDelegate> delegate; @property (nonatomic,retain) NSDate *minimumDate;
@property (nonatomic,retain) NSDate *maxmumDate;
@property (nonatomic) NSInteger minuteInterval; @end @implementation HRPDropDownDatePickerController
{
DropDownDatePickerMode _mode;
UIDatePicker *_picker;
NSDate *_lastDate;
} - (id)initWithDatePickerMode:(DropDownDatePickerMode)mode
{
if (self = [super init])
{
_mode = mode;
}
return self;
} - (void)viewDidLoad
{
_confrimButton = [[UIBarButtonItem alloc] initWithTitle:@"OK"
style:UIBarButtonItemStyleDone
target:self action:@selector(dismiss)]; self.navigationItem.rightBarButtonItem = _confrimButton; _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_titleLable.textColor = [UIColor blueColor];
_titleLable.font = [UIFont systemFontOfSize:];
self.navigationItem.titleView = _titleLable; _picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , - )];
_picker.datePickerMode = (_mode == DropDownDatePickerModeDate)?UIDatePickerModeDate:UIDatePickerModeTime;
[self.view addSubview:_picker];
} - (void)dismiss
{
_lastDate = self.currentDate; if (![self.currentDate isEqual:_lastDate])
{
if (_delegate) {
[_delegate datePickerControllerDateChanged:self.currentDate];
}
}
if (_delegate) {
[_delegate datePickerControllerNeedDismiss];
}
} - (void)setCurrentDate:(NSDate *)currentDate
{
[_picker setDate:currentDate animated:YES];
} - (void)setMaxmumDate:(NSDate *)maxmumDate
{
_picker.maximumDate = maxmumDate;
} - (void)setMinimumDate:(NSDate *)minimumDate
{
_picker.minimumDate = minimumDate;
} - (NSDate *)currentDate
{
return _picker.date;
} - (NSDate *)maxmumDate
{
return _picker.maximumDate;
} - (NSDate *)minimumDate
{
return _picker.minimumDate;
} - (void)setMinuteInterval:(NSInteger)minuteInterval
{
_picker.minuteInterval = minuteInterval;
} @end @interface DropDownDatePicker()
<HRPDropDownDatePickerControllerDelegate> @end @implementation DropDownDatePicker
{
UIButton *_button;
UIPopoverController *_popover;
HRPDropDownDatePickerController *_controller;
DropDownDatePickerMode _mode;
BOOL _needNotify;
} - (id)initWithFrame:(CGRect)frame
dateMode:(DropDownDatePickerMode)mode
{
self = [super initWithFrame:frame];
if (self) {
_needNotify = NO;
_mode = mode;
UIImage *arrowImage = [UIImage imageNamed:@"HZDropDownDatePickerDownArrow"];
CGRect imageViewFrame = CGRectMake(CGRectGetWidth(self.bounds) - arrowImage.size.width - , , arrowImage.size.width, arrowImage.size.height); UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
imageView.image = arrowImage;
imageView.backgroundColor = [UIColor redColor];
[self addSubview:imageView]; _button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.contentVerticalAlignment = UIControlContentHorizontalAlignmentLeft;
_button.titleLabel.font = [UIFont systemFontOfSize:14.0];
_button.frame = self.bounds;
[_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [_button addTarget:self
action:@selector(showPopover)
forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_button]; _controller = [[HRPDropDownDatePickerController alloc] initWithDatePickerMode:mode];
_controller.delegate = self; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:_controller];
if (!_popover) {
_popover = [[UIPopoverController alloc] initWithContentViewController:navi];
_popover.popoverContentSize = CGSizeMake(, );
}
else {
[_popover setContentViewController:navi];
}
self.currentDate = [NSDate date];
}
return self;
} - (UILabel *)titleLabel
{
return _controller.titleLable;
} - (NSDate *)currentDate{
return _controller.currentDate;
} - (void)setDateTextFont:(UIFont *)dateTextFont
{
_button.titleLabel.font = dateTextFont;
} - (UIFont *)dateTextFont
{
return _button.titleLabel.font;
} - (void)setDateTextColor:(UIColor *)dateTextColor
{
[_button setTitleColor:dateTextColor forState:UIControlStateNormal];
} - (UIColor *)dateTextColor
{
return [_button titleColorForState:UIControlStateNormal];
} - (void)setCurrentDate:(NSDate *)currentDate
{
_controller.currentDate = currentDate;
_needNotify = NO;
[self datePickerControllerDateChanged:currentDate];
} - (void)setMaximumDate:(NSDate *)maximumDate
{
_controller.maxmumDate = maximumDate;
} - (NSDate *)maximumDate
{
return _controller.minimumDate;
} - (void)setMinimumDate:(NSDate *)minimumDate
{
_controller.minimumDate = minimumDate;
} - (NSDate *)minimumDate
{
return _controller.minimumDate;
} - (NSInteger)minuteInterval{
return _controller.minuteInterval;
} - (void)setMinuteInterval:(NSInteger)minuteInterval{
_controller.minuteInterval = minuteInterval;
} -(UIBarButtonItem *)confrimButton{
return _controller.confrimButton;
} #pragma mark -
#pragma mark delegate - (void)datePickerControllerNeedDismiss
{
[_popover dismissPopoverAnimated:YES];
} - (void)datePickerControllerDateChanged:(NSDate *)newDate
{
NSString *dateString;
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
if(_mode == DropDownDatePickerModeDate){
formater.dateFormat = @"yyyy年 MM月 dd日";
}
else if(_mode == DropDownDatePickerModeTime){
formater.dateFormat = @"hh: mm";
} dateString = [formater stringFromDate:newDate];
[_button setTitle:dateString forState:UIControlStateNormal]; if(_dateChangeHandler && _needNotify){
_dateChangeHandler(newDate);
}
_needNotify = YES;
} #pragma mark Private Methods - (void)showPopover
{
CGFloat x = self.bounds.size.width / ;
CGFloat y = self.bounds.size.height / ; [_popover presentPopoverFromRect:CGRectMake(x, y, , )
inView:_button
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} @end

 


 
 
 
 
 

《简单的自定义DropDatePicker》-- UIPopoverController 和 代理 以及 Block 实现。的更多相关文章

  1. 最简单的自定义适配器adapter

    下面是一个非常简单的自定义适配器的总体源码,从这个源码入门,就可以慢慢学会适配器了 适配器的作用: 完成数据和界面控件的绑定,把数据绑定到界面的现实控件条目上(对于listView,应该是listVi ...

  2. Android自定义view(一):制作一个最最最简单的自定义view

    转载:https://blog.csdn.net/wsyizmao/article/details/78491422 浅谈安卓自定义view(一):制作一个最最最简单的自定义view 对于安卓程序员来 ...

  3. java:jsp: 一个简单的自定义标签 tld

    java:jsp: 一个简单的自定义标签 tld 请注意,uri都是:http://www.tag.com/mytag,保持统一,要不然报错,不能访问 tld文件 <?xml version=& ...

  4. js/jq基础(日常整理记录)-4-一个简单的自定义tree插件

    一.一个简单的自定义tree插件 上一篇是之前自定义的table插件,这一篇也是之前同期尝试做的一个tree插件. 话不多说,先看看长什么样子哈! 现在来看确实不好看,以后在优化吧! 数据源:ajax ...

  5. openresty开发系列2--nginx的简单安装,正向、反向代理及常用命令和信号控制介绍

    openresty开发系列2--nginx的简单安装,正向.反向代理及常用命令和信号控制介绍 一.nginx的安装下载及编译安装1.Nginx下载:nginx-1.13.0.tar.gz,下载到:/u ...

  6. iOS 代理与block 逆向传值 学习

    一般在项目中出现逆向传值的时候就需要用到代理.block 或者通知中心了.由于公司的项目底层封装的很好,所以项目做了三四个月就算碰到需要逆传的情况也不用自己处理.但是最近遇到了一个特别的情况就需要自己 ...

  7. 使用 FiddlerCore 自定义 HTTP/HTTPS 网络代理

    Fiddler 是个很好用的网络请求查看与调试工具,还可以写插件来扩展其功能. Fiddler 插件开发,使用 WPF 作为 UI 控件 - J.晒太阳的猫 - 博客园 但部分场景下,需要自定义很多网 ...

  8. 简单跳转到微信分享,基于libWeiChatSDK 和简单的自定义UIActivityViewController

    一.自定义UIActivity: 如果想要自定义UIActivity必须知道UIActivityViewController.首先这个类主要是用于接受字符串,RUL类型和图片类型的数据用于分享和操作的 ...

  9. JS简单实现自定义右键菜单

    RT,一个简单的例子,仅仅讲述原理 <div id="menu" style="width: 0;height: 0;background: cadetblue;p ...

随机推荐

  1. Python3中的字符串函数学习总结

    这篇文章主要介绍了Python3中的字符串函数学习总结,本文讲解了格式化类方法.查找 & 替换类方法.拆分 & 组合类方法等内容,需要的朋友可以参考下. Sequence Types ...

  2. linux下mono,powershell安装教程

    1简介 简单来说pash就是bash+powershell 2官网 https://github.com/Pash-Project/Pash 3下载fedora20---lxde桌面---32位版. ...

  3. 【腾讯bugly干货分享】微信Android热补丁实践演进之路

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1264& ...

  4. [异常解决] How to build a gcc toolchain for nRF51 on linux (very detailed!!!)

    1.Install gcc-arm-none-eabi https://devzone.nordicsemi.com/tutorials/7/This link shows that developm ...

  5. CSharpGL(15)用GLSL渲染2种类型的文字

    CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...

  6. Java基础之IO流

    很长时间都没有更新了,最近在补充JavaSE的一些细节部分 关于IO流的一些总结 首先要介绍的是File类,File类用于对文件和目录的一些操作 1.创建文件CreateNewFile() 2.对文件 ...

  7. 苹果台式一体机笔记本安装win双系统攻略教程

    步骤 序:win系统下载 :http://www.itellyou.cn 选择要安装的系统进行下载,本文以win7为例   进入苹果系统,左上角——前往——实用工具——BootCamp 助理 点击继续 ...

  8. awk命令简介

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...

  9. Vue1.0 的技术栈

    vuejs概述 Vue.js是用于构建交互式的Web界面的库.它提供了MVVM数据绑定和一个可组合的组件系统,具有简单.灵活的API. 结合node.js 可以实现前后端开发从物理上的分离.使前端负责 ...

  10. Java开发中的23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...