详解iOS时间选择框
本文实例为大家介绍了iOS时间选择框的示例代码,供大家参考,具体内容如下
代码:
一、头文件
#import<UIKit/UIKit.h> @classITTPickView; @protocolITTPickViewDelegate<NSObject> @optional -(void)toobarDonBtnHaveClick:(ITTPickView*)pickView resultString:(NSString*)resultString; @end @interfaceITTPickView:UIView @property(nonatomic,weak)id<ITTPickViewDelegate>delegate;//委托 /** *通过时间创建一个DatePicker * *@paramdate默认选中时间 *@paramisHaveNavControler是否在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*)
pickViewresultString:(NSString*)resultString(因为是可选的委托事件,实现了才会执行);获得选择的时间字符串。 #import"ITTPickView.h" #defineITTToobarHeight40 @interfaceITTPickView() @property(nonatomic,assign)NSDate*defaulDate;//默认时间 @property(nonatomic,strong)UIDatePicker*datePicker;//datePicker控件 @property(nonatomic,assign)NSIntegerpickeviewHeight;//pickerView的高度 @property(nonatomic,strong)UIToolbar*toolbar;//toolBar控件 @property(nonatomic,copy)NSString*resultString;//返回的时间字符串 @property(nonatomic,assign)NSIntegerselfOriginy;//当前view的frame.origin.y @property(nonatomic,assign)NSIntegerselfViewInitH;//初始状态view的frame.origin.y @end @implementationITTPickView //初始化ITTPickView, -(instancetype)initDatePickWithDate:(NSDate*)defaulDatedatePickerMode:(UIDatePickerMode)datePickerModeisHaveNavControler:(BOOL)isHaveNavControler{ self=[superinit]; if(self){ self.defaulDate=defaulDate; [selfsetUpDatePickerWithdatePickerMode:datePickerMode]; [selfsetFrameWith:isHaveNavControler]; [selfsetUpToolBar]; } returnself; } //设定ITTPickView的frame大小 -(void)setFrameWith:(BOOL)isHaveNavControler{ CGFloattoolViewX=0; CGFloattoolViewH=self.pickeviewHeight+ITTToobarHeight; CGFloattoolViewY; if(isHaveNavControler){ toolViewY=[UIScreenmainScreen].bounds.size.height-toolViewH-50; }else{ toolViewY=[UIScreenmainScreen].bounds.size.height-toolViewH; } CGFloattoolViewW=[UIScreenmainScreen].bounds.size.width; CGFloattoolViewInitH=[UIScreenmainScreen].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=[[UIDatePickeralloc]init]; datePicker.locale=[[NSLocalealloc]initWithLocaleIdentifier:@"zh-CN"]; datePicker.datePickerMode=datePickerMode; datePicker.backgroundColor=[UIColorwhiteColor]; if(self.defaulDate){ [datePickersetDate:self.defaulDate]; } self.datePicker=datePicker; datePicker.frame=CGRectMake(0,ITTToobarHeight,[UIScreenmainScreen].bounds.size.width,datePicker.frame.size.height); self.pickeviewHeight=datePicker.frame.size.height; [selfaddSubview:datePicker]; } //设置toolBar的各个属性,并作为view的子视图 -(void)setUpToolBar{ self.toolbar=[selfsetToolbarStyle]; [selfsetToolbarWithPickViewFrame]; [selfaddSubview:self.toolbar]; } //设置toolBar的样式 -(UIToolbar*)setToolbarStyle{ UIToolbar*toolbar=[[UIToolbaralloc]init]; UIBarButtonItem*left=[[UIBarButtonItemalloc]initWithTitle:@"取消"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(removeView)]; UIBarButtonItem*centerSpace=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil]; UIBarButtonItem*right=[[UIBarButtonItemalloc]initWithTitle:@"确定"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(doneClick)]; toolbar.items=@[lefttem,centerSpace,right]; returntoolbar; } //设定tooBar的frame大小 -(void)setToolbarWithPickViewFrame{ self.toolbar.frame=CGRectMake(0,0,[UIScreenmainScreen].bounds.size.width,ITTToobarHeight); } //点击确定按钮 -(void)doneClick{ if(self.datePicker){ NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init]; dateFormatter.dateFormat=@"yyyy-MM-dd"; self.resultString=[dateFormatterstringFromDate:self.datePicker.date]; } if([self.delegaterespondsToSelector:@selector(toobarDonBtnHaveClick:resultString:)]){ [self.delegatetoobarDonBtnHaveClick:selfresultString:self.resultString]; } [selfremoveView]; } /** *从窗口移除本控件 */ -(void)removeView{ [UIViewanimateWithDuration:0.25fanimations:^{ self.frame=CGRectMake(self.frame.origin.x,self.selfViewInitH,self.frame.size.width,self.frame.size.height); }completion:^(BOOLfinished){ if(finished){ [selfremoveFromSuperview]; } }]; } /** *在窗口显示本控件 */ -(void)showView{ [[UIApplicationsharedApplication].keyWindowaddSubview:self]; [UIViewanimateWithDuration:0.25fanimations:^{ self.frame=CGRectMake(self.frame.origin.x,self.selfOriginy,self.frame.size.width,self.frame.size.height); }completion:^(BOOLfinished){ }]; } @end
三、运用ITTPickView
UIButton*testBitton=[[UIButtonalloc]initWithFrame:CGRectMake(0,450,111,40)]; [testBittonsetBackgroundColor:[UIColorredColor]]; [testBittonaddTarget:selfaction:@selector(test)forControlEvents:UIControlEventTouchUpInside]; //点击按钮弹出时间选择框 -(void)test{ ITTPickView*datePicker=[[ITTPickViewalloc]initDatePickWithDate:[NSDatedate]datePickerMode:UIDatePickerModeDateisHaveNavControler:NO]; [datePickershowView]; }
以上就是本文的全部内容,希望对大家的学习有所帮助。