iPhone display date picker on UITextField touch

后端 未结 8 1347
一生所求
一生所求 2020-11-30 00:08

I followed this thread: datepicker by clicking on textfield

I imported both of the following protocols:

@interface ViewController : UIViewController&         


        
相关标签:
8条回答
  • 2020-11-30 00:52

    Please go through this Process In .h file create object for UIDatePicker

    UIDatePicker *datePicker;
    

    in .m file viewDidloadMethod

    datePicker = [[UIDatePicker alloc]init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.minimumDate = [NSDate date];
    [_dateTxtFld setInputView:datePicker];
    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [toolBar setTintColor:[UIColor grayColor]];
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(showSelectedDate)];
    UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
    
    [_dateTxtFld setInputAccessoryView:toolBar];
    

    after that you can create ShowSele

    -(void)showSelectedDate {
        if ([_dateTxtFld isFirstResponder]) {
            NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"YYYY-MM-dd"];
            _dateTxtFld.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]];
           [_dateTxtFld resignFirstResponder];
        }
       else if ([_timeTxtFld isFirstResponder]) {
       }
    }
    

    It's works for me.

    0 讨论(0)
  • 2020-11-30 00:53

    A nice approach is to make your own class (I called it TextFieldFormDate) that is derived from UITextField and encapsulates all the required behaviour. In your storyboard you then set the class for the text field to TextFieldFormDate (in my case).

    Use didChange and didSelect in your ViewController (that is using the TextFieldFormDate) to be notified of changes and when date is actually picked.

    class TextFieldFormDate: UITextField {
    
        var date : Date {
            set {
               picker.date = newValue
               text = newValue.localizedDateOnly()
            }
    
            get {
                return picker.date
            }
        }
    
        //public handlers
        var didChange : ( (Date)->() )?
        var didSelect : ( (Date)->() )?
    
        private var picker : UIDatePicker {
            return inputView as! UIDatePicker
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
            setup()
        }
    
        private func setup() {
            font = UIFont.defaultFont()
            //picker for datefield
            inputView = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            picker.datePickerMode = .date
            picker.addTarget(self, action: #selector(pickerChanged), for: .valueChanged)
            let toolBar = UIToolbar()
            toolBar.tintColor = PaintCode.mainBlue
            toolBar.items = [
                UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
                BarBtnPaintCode(type: "done", target: self, action: #selector(didPressDone))
            ]
            toolBar.sizeToFit()
            inputAccessoryView = toolBar
        }
    
        @objc private func pickerChanged() {
            text = date.localizedDateOnly()
            didChange?(picker.date)
        }
    
        @objc private func didPressDone() {
            text = date.localizedDateOnly()
            resignFirstResponder()
            didSelect?(picker.date)
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题