你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 詳解iOS時光選擇框

詳解iOS時光選擇框

編輯:IOS開發綜合

本文實例為年夜家引見了IOS時光選擇框的示例代碼,供年夜家參考,詳細內容以下

代碼:

1、頭文件

#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

2、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

3、應用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];
}

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

【詳解iOS時光選擇框】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved