LoginSignup
4
5

More than 5 years have passed since last update.

[Swift]UIDatePickerの値が変更されたら何かする

Last updated at Posted at 2017-05-09

UIDatePickerの値が変更されたら何かする例を紹介します。

やり方

Target-Actionデザインパターンでアプリに通知する
valueChangedイベント)

  • addTarget(_:action:for:)メソッドを利用する
  • Interface Builde@IBActionを利用する

実装例

// IB側のUIDatePickerと接続
/// datePickerの値が変更されたら呼ばれる
@IBAction private func didValueChangedDatePicker(_ sender: UIDatePicker) {
    // ここにやりたい処理を書く
    print(createDateString(date: sender.date, calendar: sender.calendar))
}

補足

createDateString(date: , calendar: )メソッドについては[Swift]weekdaySymbols・shortWeekdaySymbolsから曜日を表示する際の注意点を参照ください。

サンプルコード

サンプルコードをGitHubにあげたので、実際に触りたい方はこちらからどうぞ。
https://github.com/taguchi-k/ios-weekday-from-datepicker

開発環境

category Version
Swift 3.1
XCode 8.3.2

参考

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5