在Swift中的弹出datePickerView中添加一个Done按钮?

时间:2015-01-20 14:57:53

标签: ios swift uitextfield uidatepicker

我想在Swift中弹出的datePickerView中添加一个Done按钮。

以下是代码:

@IBOutlet var datePicker: UITextField!

@IBAction func dateTextInputPressed(sender: UITextField) {

    var datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date

    sender.inputView = datePickerView
    datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
}

func handleDatePicker(sender: UIDatePicker) {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    datePicker.text = dateFormatter.stringFromDate(sender.date)
}

我可以使用此代码成功弹出datePickerView。

result of datePickerView

但是在我选择了这个日期后,它没有"完成"按钮解除它。

那么如何在其中添加完成按钮?

谢谢!

5 个答案:

答案 0 :(得分:29)

我做了这个扩展来关闭选择器。 Swift 2

extension UIToolbar {

 func ToolbarPiker(mySelect : Selector) -> UIToolbar {

    let toolBar = UIToolbar()

    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor.blackColor()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: mySelect)
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

    toolBar.setItems([ spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

    return toolBar
}

}

斯威夫特3 - 4

extension UIToolbar {

func ToolbarPiker(mySelect : Selector) -> UIToolbar {

    let toolBar = UIToolbar()

    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor.black
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: mySelect)
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

    toolBar.setItems([ spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    return toolBar
}

}

您只需在viewController中使用它:

  override func viewDidLoad() {
    super.viewDidLoad()

 //SWIFT2
 /*
let toolBar = UIToolbar().ToolbarPiker(#selector(MyViewController.dismissPicker))

 */
// Swift3 - 4
    let toolBar = UIToolbar().ToolbarPiker(mySelect: #selector(MyViewController.dismissPicker))

    myTextField.inputAccessoryView = toolBar


}

 // Swift 2 - 3
 func dismissPicker() { 

    view.endEditing(true)

}

 // Swift 4
 @objc func dismissPicker() { 

    view.endEditing(true)

}

答案 1 :(得分:14)

您可以对UIView的{​​{1}}属性使用通用inputView。我们可以将UITextFieldUIDatePicker添加到此UIButton

UIView

答案 2 :(得分:6)

如果您使用的是按钮而不是文本字段:

/ **     * MARK - 提前预订行动     * /

@IBAction func advanceBookingAction(sender: UIButton) {

    var datePicker : UIDatePicker = UIDatePicker()
    var datePickerContainer = UIView()

    datePickerContainer.frame = CGRectMake(0.0, self.view.frame.height/2, 320.0, 300.0)
    datePickerContainer.backgroundColor = UIColor.whiteColor()

    var pickerSize : CGSize = datePicker.sizeThatFits(CGSizeZero)
    datePicker.frame = CGRectMake(0.0, 20, pickerSize.width, 460)
    datePicker.setDate(NSDate(), animated: true)
    datePicker.maximumDate = NSDate()
    datePicker.datePickerMode = UIDatePickerMode.DateAndTime
    datePicker.addTarget(self, action: "dateChangedInDate:", forControlEvents: UIControlEvents.ValueChanged)
    datePickerContainer.addSubview(datePicker)

    var doneButton = UIButton()
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    doneButton.addTarget(self, action: Selector("dismissPicker:"), forControlEvents: UIControlEvents.TouchUpInside)
    doneButton.frame    = CGRectMake(250.0, 5.0, 70.0, 37.0)

    datePickerContainer.addSubview(doneButton)

    self.view.addSubview(datePickerContainer)

}// end advanceBookingAction

/**
* MARK - observer to get the change in date
*/

func dateChangedInDate(sender:UIDatePicker){

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    objMethodInc.printvj("date selected \(datePicker.date)")

}// end dateChangedInDate

/*
* MARK - dismiss the date picker value
*/
func dismissPicker(sender: UIButton) {
    objMethodInc.printvj("dismiss date picker")
    datePickerContainer.removeFromSuperview()
}// end dismissPicker

答案 3 :(得分:0)

在Swift 5中

如果只希望DatePicker上的完成按钮使用我的代码

//Write toolbar code for done button
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onClickDoneButton))
toolBar.setItems([space, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
dobTF.inputAccessoryView = toolBar //Change your TextField name here

@objc func onClickDoneButton() {
    self.view.endEditing(true)
}

答案 4 :(得分:0)

class _MyViewState extends State<MyView> {
  ValueNotifier<MyClass> notifier;
  Timer _timer;

  ....

  @override
  initState() {
    super.initState();

    notifier = ValueNotifier<MyClass>(MyClass());
    _timer = Timer.periodic(Duration(milliseconds: 10), _updateData);
  }


  _updateData(Timer t) {
     var newValue = getNewValueSomewhere();
     notifier.value.changeValue(newValue);
  }