Created
April 12, 2017 03:11
-
-
Save jhonny-me/8cffed8f37b727496d659d0e06ad3d4a to your computer and use it in GitHub Desktop.
UIDatePicker sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// demo | |
// | |
// Created by Johnny Gu on 05/12/2016. | |
// Copyright © 2016 Johnny Gu. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var startTextField: UITextField! | |
@IBOutlet weak var endTextField: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let picker = UIDatePicker() | |
picker.addTarget(self, action: #selector(pickerViewValueDidChanged), for: .valueChanged) | |
startTextField.inputView = picker | |
endTextField.inputView = picker | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
super.touchesBegan(touches, with: event) | |
view.endEditing(true) | |
} | |
func pickerViewValueDidChanged(_ sender: UIDatePicker) { | |
if startTextField.isEditing { | |
startTextField.text = sender.date.description | |
}else if endTextField.isEditing { | |
endTextField.text = sender.date.description | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment