今天就跟大家聊聊有關(guān)iOS中怎么自定義一個(gè)日期選擇器,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站主營(yíng)瑞昌網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開(kāi)發(fā),瑞昌h5微信平臺(tái)小程序開(kāi)發(fā)搭建,瑞昌網(wǎng)站營(yíng)銷推廣歡迎瑞昌等地區(qū)企業(yè)咨詢一、封裝日期選擇器類YCDatePickerView
1、新建一個(gè)類,基于UIView,取名YCDatePickerView。
2、YCDatePickerView類中.h文件代碼如下:
typedef void (^MyBasicBlock)(id result); #import <UIKit/UIKit.h> @interface YCDatePickerView : UIView @property (nonatomic, strong) UIButton *btnConfirm; @property (nonatomic, strong) UIButton *btnCancel; @property (nonatomic, strong) UIDatePicker *datePicker; @property (nonatomic, copy) MyBasicBlock selectBlock; + (YCDatePickerView *)datePickerViewWithMode:(UIDatePickerMode) datePickerMode bolck:(MyBasicBlock)block; @end
3、YCDatePickerView類中.m文件代碼如下:
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height #define kTopBarViewHeight 40 #define kButton_Width 40 #define kButton_Height 40 #define kDatePicker_Height 256 #import "YCDatePickerView.h" @implementation YCDatePickerView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UIView *topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kTopBarViewHeight)]; topBarView.backgroundColor = [UIColor orangeColor]; [self addSubview:topBarView]; _btnConfirm = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-kButton_Width-10, 0, kButton_Width, kButton_Height)]; [_btnConfirm addTarget:self action:@selector(btnConfirm:) forControlEvents:UIControlEventTouchUpInside]; [_btnConfirm setTitle:@"確定" forState:UIControlStateNormal]; [topBarView addSubview:_btnConfirm]; _btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, kButton_Width, kButton_Height)]; [_btnCancel addTarget:self action:@selector(btnCancel:) forControlEvents:UIControlEventTouchUpInside]; [_btnCancel setTitle:@"取消" forState:UIControlStateNormal]; [topBarView addSubview:_btnCancel]; _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topBarView.frame), self.frame.size.width, self.frame.size.height-kTopBarViewHeight)]; _datePicker.backgroundColor = [UIColor whiteColor]; [self addSubview:_datePicker]; } return self; } - (void)btnConfirm:(id)sender { if (self.selectBlock) { self.selectBlock(self.datePicker.date); } } - (void)btnCancel:(id)sender { if (self.selectBlock) { self.selectBlock(nil); } } + (YCDatePickerView *)datePickerViewWithMode:(UIDatePickerMode) datePickerMode bolck:(MyBasicBlock)block { YCDatePickerView *picker = [[YCDatePickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDatePicker_Height)]; picker.datePicker.datePickerMode = datePickerMode; picker.selectBlock = block; return picker; } @end
二、YCDatePickerView的使用
1、在ViewController中導(dǎo)入頭文件
#import "YCDatePickerView.h"
2、在ViewController.m中添加如下代碼
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height #import "ViewController.h" #import "YCDatePickerView.h" @interface ViewController () @property (retain, nonatomic) YCDatePickerView *datePicker; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField *begin = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH-20, 30)]; begin.placeholder = @"請(qǐng)輸入開(kāi)始時(shí)間"; begin.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:begin]; __weak ViewController *weakself = self; begin.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate bolck:^(NSDate *result) { if (result) { begin.text = [weakself dateToString:result]; } [begin resignFirstResponder]; }]; UITextField *end = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, SCREEN_WIDTH-20, 30)]; end.placeholder = @"請(qǐng)輸入結(jié)束時(shí)間"; end.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:end]; end.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate bolck:^(NSDate *result) { if (result) { end.text = [weakself dateToString:result]; } [end resignFirstResponder]; }]; } //日期轉(zhuǎn)為字符串 - (NSString *)dateToString:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *strDate = [dateFormatter stringFromDate:date]; return strDate; } @end
看完上述內(nèi)容,你們對(duì)iOS中怎么自定義一個(gè)日期選擇器有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
新聞標(biāo)題:iOS中怎么自定義一個(gè)日期選擇器-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article18/hoddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、手機(jī)網(wǎng)站建設(shè)、云服務(wù)器、營(yíng)銷型網(wǎng)站建設(shè)、App開(kāi)發(fā)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容