Skip to content

Instantly share code, notes, and snippets.

@matsuhisa
Created February 11, 2015 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsuhisa/7c326646a6492a133b81 to your computer and use it in GitHub Desktop.
Save matsuhisa/7c326646a6492a133b81 to your computer and use it in GitHub Desktop.
Swiftで UITextField から UIDatePicker を呼び出して Toolbarに完了ボタンなどを用意する ref: http://qiita.com/matsuhisa@github/items/4bb9803828efb89e0305
import UIKit
class ViewController: UIViewController, UIToolbarDelegate {
@IBOutlet weak var textField: UITextField!
//var textField: UITextField!
var toolBar:UIToolbar!
var myDatePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
// 入力欄の設定
//textField = UITextField(frame: CGRectMake(self.view.frame.size.width/3, 100, 0, 0))
textField.placeholder = dateToString(NSDate())
textField.text = dateToString(NSDate())
//textField.sizeToFit()
self.view.addSubview(textField)
// UIDatePickerの設定
myDatePicker = UIDatePicker()
myDatePicker.addTarget(self, action: "changedDateEvent:", forControlEvents: UIControlEvents.ValueChanged)
myDatePicker.datePickerMode = UIDatePickerMode.Date
textField.inputView = myDatePicker
// UIToolBarの設定
toolBar = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height/6, self.view.frame.size.width, 40.0))
toolBar.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)
toolBar.barStyle = .BlackTranslucent
toolBar.tintColor = UIColor.whiteColor()
toolBar.backgroundColor = UIColor.blackColor()
let toolBarBtn = UIBarButtonItem(title: "完了", style: .Bordered, target: self, action: "tappedToolBarBtn:")
let toolBarBtnToday = UIBarButtonItem(title: "今日", style: .Bordered, target: self, action: "tappedToolBarBtnToday:")
toolBarBtn.tag = 1
toolBar.items = [toolBarBtn, toolBarBtnToday]
textField.inputAccessoryView = toolBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// 「完了」を押すと閉じる
func tappedToolBarBtn(sender: UIBarButtonItem) {
textField.resignFirstResponder()
}
// 「今日」を押すと今日の日付をセットする
func tappedToolBarBtnToday(sender: UIBarButtonItem) {
myDatePicker.date = NSDate()
changeLabelDate(NSDate())
}
//
func changedDateEvent(sender:AnyObject?){
var dateSelecter: UIDatePicker = sender as UIDatePicker
self.changeLabelDate(myDatePicker.date)
}
func changeLabelDate(date:NSDate) {
textField.text = self.dateToString(date)
}
func dateToString(date:NSDate) ->String {
let calender: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let comps: NSDateComponents = calender.components(NSCalendarUnit.YearCalendarUnit|NSCalendarUnit.MonthCalendarUnit|NSCalendarUnit.DayCalendarUnit|NSCalendarUnit.HourCalendarUnit|NSCalendarUnit.MinuteCalendarUnit|NSCalendarUnit.SecondCalendarUnit|NSCalendarUnit.WeekdayCalendarUnit, fromDate: date)
var date_formatter: NSDateFormatter = NSDateFormatter()
var weekdays: Array = [nil, "", "", "", "", "", "", ""]
date_formatter.locale = NSLocale(localeIdentifier: "ja")
date_formatter.dateFormat = "yyyy年MM月dd日(\(weekdays[comps.weekday])"
return date_formatter.stringFromDate(date)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment