欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解iOS时间选择框

程序员文章站 2023-11-04 12:58:22
本文实例为大家介绍了ios时间选择框的示例代码,供大家参考,具体内容如下 代码: 一、头文件 #import ...

本文实例为大家介绍了ios时间选择框的示例代码,供大家参考,具体内容如下

代码:

一、头文件

#import <uikit/uikit.h>
@class ittpickview;
@protocol ittpickviewdelegate <nsobject>
@optional
-(void)toobardonbtnhaveclick:(ittpickview *)pickview
resultstring:(nsstring *)resultstring;
@end
@interface ittpickview : uiview
@property(nonatomic,weak) id<ittpickviewdelegate> delegate;//委托
/**
* 通过时间创建一个datepicker
*
* @param date 默认选中时间
* @param ishavenavcontroler是否在navcontroler之内
*
* @return 带有toolbar的datepicker
*/
-(instancetype)initdatepickwithdate:(nsdate *)defauldate
datepickermode:(uidatepickermode)datepickermode
ishavenavcontroler:(bool)ishavenavcontroler;
/**
* 从窗口移除本控件
*/
-(void)removeview;
/**
* 在窗口显示本控件
*/
-(void)showview;
@end

二、ittpickview的实现,主要的控件uitoolbar、uidatepicker,点击确定后执行-(void)toobardonbtnhaveclick:(ittpickview *)

pickview resultstring:(nsstring *)resultstring(因为是可选的委托事件,实现了才会执行);获得选择的时间字符串。
#import "ittpickview.h"
#define itttoobarheight 40
@interface ittpickview ()
@property (nonatomic,assign) nsdate *defauldate;//默认时间
@property (nonatomic,strong) uidatepicker *datepicker;//datepicker控件
@property (nonatomic,assign) nsinteger pickeviewheight;//pickerview的高度
@property (nonatomic,strong) uitoolbar *toolbar;//toolbar控件
@property (nonatomic,copy) nsstring *resultstring;//返回的时间字符串
@property (nonatomic,assign) nsinteger selforiginy;//当前view的frame.origin.y
@property (nonatomic,assign) nsinteger selfviewinith;//初始状态view的frame.origin.y
@end
@implementation ittpickview
//初始化ittpickview,
- (instancetype)initdatepickwithdate:(nsdate *)defauldate datepickermode:(uidatepickermode)datepickermode ishavenavcontroler:(bool)ishavenavcontroler {
self = [super init];
if (self) {
self.defauldate = defauldate;
[self setupdatepickerwithdatepickermode:datepickermode];
[self setframewith:ishavenavcontroler];
[self setuptoolbar];
}
return self;
}
//设定ittpickview的frame大小
-(void)setframewith:(bool)ishavenavcontroler {
cgfloat toolviewx = 0;
cgfloat toolviewh = self.pickeviewheight + itttoobarheight;
cgfloat toolviewy;
if (ishavenavcontroler) {
toolviewy = [uiscreen mainscreen].bounds.size.height - toolviewh - 50;
}else {
toolviewy = [uiscreen mainscreen].bounds.size.height - toolviewh;
}
cgfloat toolvieww = [uiscreen mainscreen].bounds.size.width;
cgfloat toolviewinith = [uiscreen mainscreen].bounds.size.height;
self.selfviewinith = toolviewinith;//初始状态view的frame.origin.y
self.selforiginy = toolviewy;//当前view的frame.origin.y
self.frame = cgrectmake(toolviewx, toolviewinith, toolvieww, toolviewh);
}
//设定datepicker控件的样式以及frame大小,并作为view的子视图
-(void)setupdatepickerwithdatepickermode:(uidatepickermode)datepickermode {
uidatepicker *datepicker = [[uidatepicker alloc] init];
datepicker.locale = [[nslocale alloc] initwithlocaleidentifier:@"zh-cn"];
datepicker.datepickermode = datepickermode;
datepicker.backgroundcolor = [uicolor whitecolor];
if (self.defauldate) {
[datepicker setdate:self.defauldate];
}
self.datepicker = datepicker;
datepicker.frame = cgrectmake(0, itttoobarheight, [uiscreen mainscreen].bounds.size.width, datepicker.frame.size.height);
self.pickeviewheight = datepicker.frame.size.height;
[self addsubview:datepicker];
}
//设置toolbar的各个属性,并作为view的子视图
- (void)setuptoolbar {
self.toolbar = [self settoolbarstyle];
[self settoolbarwithpickviewframe];
[self addsubview:self.toolbar];
}
//设置toolbar的样式
-(uitoolbar *)settoolbarstyle {
uitoolbar *toolbar = [[uitoolbar alloc] init];
uibarbuttonitem *left = [[uibarbuttonitem alloc] initwithtitle:@" 取消 " style:uibarbuttonitemstyleplain target:self action:@selector(removeview)];
uibarbuttonitem *centerspace = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:self action:nil];
uibarbuttonitem *right = [[uibarbuttonitem alloc] initwithtitle:@" 确定 " style:uibarbuttonitemstyleplain target:self action:@selector(doneclick)];
toolbar.items = @[lefttem, centerspace, right];
return toolbar;
}
//设定toobar的frame大小
- (void)settoolbarwithpickviewframe {
self.toolbar.frame = cgrectmake(0, 0,[uiscreen mainscreen].bounds.size.width, itttoobarheight);
}
//点击确定按钮
-(void)doneclick {
if (self.datepicker) {
nsdateformatter *dateformatter = [[nsdateformatter alloc] init];
dateformatter.dateformat = @"yyyy-mm-dd";
self.resultstring = [dateformatter stringfromdate:self.datepicker.date];
}
if ([self.delegate respondstoselector:@selector(toobardonbtnhaveclick:resultstring:)]) {
[self.delegate toobardonbtnhaveclick:self resultstring:self.resultstring];
}
[self removeview];
}
/**
* 从窗口移除本控件
*/
- (void)removeview {
[uiview animatewithduration:0.25f animations:^{
self.frame = cgrectmake(self.frame.origin.x, self.selfviewinith, self.frame.size.width, self.frame.size.height);
} completion:^(bool finished) {
if (finished) {
[self removefromsuperview];
}
}];
}
/**
* 在窗口显示本控件
*/
- (void)showview {
[[uiapplication sharedapplication].keywindow addsubview:self];
[uiview animatewithduration:0.25f animations:^{
self.frame = cgrectmake(self.frame.origin.x, self.selforiginy, self.frame.size.width, self.frame.size.height);
} completion:^(bool finished) {
}];
}
@end

三、运用ittpickview

uibutton *testbitton = [[uibutton alloc] initwithframe:cgrectmake(0, 450, 111, 40)];
[testbitton setbackgroundcolor:[uicolor redcolor]];
[testbitton addtarget:self action:@selector(test) forcontrolevents:uicontroleventtouchupinside];
//点击按钮弹出时间选择框
- (void)test {
ittpickview *datepicker = [[ittpickview alloc] initdatepickwithdate:[nsdate date] datepickermode:uidatepickermodedate ishavenavcontroler:no];
[datepicker showview];
}

以上就是本文的全部内容,希望对大家的学习有所帮助。