日期范围选择公用函数,传参为选择的textField

- (void)viewDidLoad

{

    [super viewDidLoad]; 

    //初始日期

    beginDateTextField_ = [ [ UITextField alloc ] init ];

    beginDateTextField_.frame = CGRectMake( newPoint.x , newPoint.y , dudaoTextFieldWidth , dudaoTextFieldHeight );

    beginDateTextField_.borderStyle = UITextBorderStyleRoundedRect;

    beginDateTextField_.font =[ UIFont fontWithName: @"Helvetica" size: 16 ];

    beginDateTextField_.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    //文本输入框添加按钮

    beginDateButton = [ UIButton buttonWithType: UIButtonTypeDetailDisclosure ];

    [ beginDateButton setImage: buttonImage forState: UIControlStateNormal ];

    [ beginDateButton addTarget: self

                         action: @selector(selectButton:) 

                forControlEvents: UIControlEventTouchUpInside ];

    beginDateTextField_.rightViewMode = UITextFieldViewModeAlways;

    beginDateTextField_.rightView = beginDateButton;

    beginDateTextField_.delegate = self;

    [ self.view addSubview: beginDateTextField_ ];


    //初始化存放选择的textfield

    selectedTextField = [ [ UITextField alloc ] init ];


}


//************优化日期选择代码***************//


//显示日期选择

- (void)dateButtonDidPush

{

    //设置完成按钮

    UIBarButtonItem* beginDoneButton = 

    [ [ [ UIBarButtonItem alloc

       initWithBarButtonSystemItem: UIBarButtonSystemItemDone

       target: self

       action: @selector(doneDidPush) ] autorelease ];

    [ self setToolbarItems: [ NSArray arrayWithObject: beginDoneButton ] animated: YES ];

    [ UIView beginAnimations: nil context: nil ];

    [ UIView setAnimationDuration: 0.3 ];

    //时间选择器上移

    datePicker_.frame = CGRectMake( 0 , 216 , 320 , 216 );

    //工具条上移   

    CGRect toolbarFrame = self.navigationController.toolbar.frame;

    toolbarFrame.origin.y = self.view.frame.size.height  - toolbarFrame.size.height - 50 ;

    self.navigationController.toolbar.frame = toolbarFrame;

    

    [ UIView commitAnimations ];

}


//日期选择完成按钮

- (void)doneDidPush

{

    NSDate* date = [ datePicker_ date ];

    NSDateFormatter* dateFormatter = [ [ NSDateFormatter alloc ] init ];

    [ dateFormatter setDateFormat: @"yyyy-MM-dd" ];;

    selectedTextField.text = [ dateFormatter stringFromDate: date ];

    [ UIView beginAnimations: nil context: nil ];

    [ UIView setAnimationDuration: 0.3 ];

    //隐藏工具条

    CGRect toolbarFrame = self.navigationController.toolbar.frame;

    toolbarFrame.origin.y

    self.view.bounds.size.height + 65 ;

    self.navigationController.toolbar.frame = toolbarFrame;

    //隐藏完成按钮

    UIBarButtonItem* spaceButton = [ [ [ UIBarButtonItem alloc

                                      initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace

                                      target: nil action: nil ] autorelease ];

    [ self setToolbarItems: [ NSArray arrayWithObject: spaceButton ] animated: YES ];

    //隐藏日期选择器

    datePicker_.frame = toolbarFrame;

    [ UIView commitAnimations ];

}


//判断是那一个textfield中按钮,并把相应textfield传给 selectedTextField

- (void)selectButton:(UIButton*)button

{

    if ( button == beginDateButton )

    {

        selectedTextField = beginDateTextField_;

    }

    else

    {

        selectedTextField = endDateTextField_;

    }

    [ self dateButtonDidPush ];

}


//***************************************//





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Material UI 5.12.2 版本的日期选择器,您需要使用 DatePicker 组件。下面是一个示例代码,展示了如何使用 DatePicker 组件: ```jsx import React, { useState } from 'react'; import { DatePicker } from '@mui/lab'; import AdapterDateFns from '@mui/lab/AdapterDateFns'; import LocalizationProvider from '@mui/lab/LocalizationProvider'; function App() { const [selectedDate, setSelectedDate] = useState(null); const handleDateChange = (date) => { setSelectedDate(date); }; return ( <LocalizationProvider dateAdapter={AdapterDateFns}> <DatePicker label="Select Date" value={selectedDate} onChange={handleDateChange} renderInput={(params) => <TextField {...params} />} /> </LocalizationProvider> ); } export default App; ``` 在这个示例中,我们导入了 DatePicker 组件、AdapterDateFns 和 LocalizationProvider 组件。DatePicker 组件用于显示日期选择器,并且会根据用户的选择更新选定的日期。AdapterDateFns 组件用于将日期适配到 DateFns 库中,LocalizationProvider 组件用于为 DatePicker 提供本地化设置。 在 App 组件中,我们创建了一个名为 selectedDate 的状态变量,并将其初始设置为 null。我们还创建了一个名为 handleDateChange 的函数,用于更新 selectedDate 的。在返回的 JSX 中,我们将 DatePicker 组件包裹在 LocalizationProvider 组件中,并将 AdapterDateFns 组件作为其 dateAdapter 属性的传递。我们将 selectedDate 绑定到 DatePicker 组件的 value 属性,并将 handleDateChange 函数绑定到 onChange 属性。我们还使用 renderInput 属性将 TextField 组件传递给 DatePicker 组件,以便为其提供输入框。 以上就是使用 Material UI 5.12.2 版本的 DatePicker 组件的示例代码。您可以根据自己的需要进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值