![見出し画像](https://assets.st-note.com/production/uploads/images/100549129/rectangle_large_type_2_91903f9feb6f515b4a0dd3494cb25058.png?width=1200)
【徒然iOS】気ままにUIKit46〜Date Pickerの使い方。ホイールを回して時刻や日付を設定する。〜
概要
このマガジンは四十を過ぎたおっさんが、
を参考にStoryboardでiOSアプリを完全に趣味で楽しんでいるだけな記事を気ままに上げてます。
今回
をレッツゴ🕺
前準備
前回はPlaygroundで完結したので念の為、
バックアップ
だけいつも通りやってから本題へ💃
![](https://assets.st-note.com/img/1679107508448-swdyyWXmoO.png?width=1200)
本題の前に、
DatePickerについては、
タッチコントロールなんかでも書いてるんだけど、
日付をひとつ選択させる場合:Picker
日付を複数選択させる場合:Calendar
が一般的な感覚。
なんで?全部カレンダーを表示させて、選ばせた方がいいじゃん!!
て人も、普段PCでやってる人なら思うと思うんだけど、理由は簡単
スマホやタブレットの画格が小さいから!!!!
calendarで選ばせると、マス目も文字も小さくなって見にくい
なので、できればPickerを使うことを推奨してる様子。
(ま、それだけで公開申請をリジェクトされることはないと思うんだけどね💦)
本題
デートピッカーとは、
ホイールを回して時刻や日付を設定するための部品
ここからは前回、用意してたヤツにまんま作り込んできまする🙇
⒈デートピッカーを配置
![](https://assets.st-note.com/img/1679107944340-nPWM4ruP2a.png?width=1200)
![](https://assets.st-note.com/img/1679107963744-DExpm9aqbb.png?width=1200)
⒉コードにアクション接続
![](https://assets.st-note.com/img/1679108132348-UqwQWLmAJb.png?width=1200)
![](https://assets.st-note.com/img/1679108150220-V7tiqx1hF7.png?width=1200)
⒊ラベルをデバイス画面に配置して、アウトレット接続
![](https://assets.st-note.com/img/1679108278116-gzkK0OHpsq.png?width=1200)
![](https://assets.st-note.com/img/1679108295006-5r5OoK9seX.png?width=1200)
![](https://assets.st-note.com/img/1679108311378-0BRu9hSPpA.png?width=1200)
![](https://assets.st-note.com/img/1679108325901-6rrKutvzuG.png?width=1200)
⒋以下のコードを組み込む
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testLabel: UILabel!
//データ変更時の呼び出しメソッド
@IBAction func changeDate(sender: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
testLabel.text = formatter.stringFromDate(sender.date)
}
//最初からあるメソッド
override func viewDidLoad() {
super.viewDidLoad()
}
}
組み込むと、、
![](https://assets.st-note.com/img/1679108460747-Ikoff2D0Yg.png?width=1200)
![](https://assets.st-note.com/img/1679108497505-4hYQVu3i8l.png?width=1200)
![](https://assets.st-note.com/img/1679108530375-CUvqD8nZx0.png?width=1200)
⒌シミュレータで実行
![](https://assets.st-note.com/img/1679108636873-suoRGh2zhp.png?width=1200)
![](https://assets.st-note.com/img/1679108717117-LWDcRRpBuD.png)
Wheelsに変更🕺
![](https://assets.st-note.com/img/1679108764728-ZU8BMPeuCc.png?width=1200)
![](https://assets.st-note.com/img/1679108835885-SzJs41UsQZ.png?width=1200)
⒍続きは、
アトリビュートインスペクターでの設定のようなので、一覧表で
![](https://assets.st-note.com/img/1679109264175-6APg0R6FAD.png?width=1200)
⒎カウントダウンタイマー
![](https://assets.st-note.com/img/1679109442829-Xf8fDpCs6Z.png?width=1200)
![](https://assets.st-note.com/img/1679109458823-duuXaQ8zli.png?width=1200)
![](https://assets.st-note.com/img/1679109471151-a6Y4sMYZ6p.png?width=1200)
![](https://assets.st-note.com/img/1679109542945-AmkOBOgsxK.png?width=1200)
![](https://assets.st-note.com/img/1679109563045-UL6r2dfrnm.png?width=1200)
![](https://assets.st-note.com/img/1679109650500-hbL6OgYaCC.png?width=1200)
//
// ViewController.swift
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testLabel: UILabel!
@IBOutlet weak var testDatePicker: UIDatePicker!
var timer:NSTimer = NSTimer()
var count:Int = 0
//データ変更時の呼び出しメソッド
@IBAction func changeDate(sender: UIDatePicker) {
//何もしない。
}
//ボタン押下時の呼び出しメソッド
@IBAction func startCountDown(sender: UIButton) {
//すでに動いているタイマーは停止する
timer.invalidate()
//カウントダウンする秒数を取得する。
count = Int(testDatePicker.countDownDuration)
//1秒周期でcountDownメソッドを呼び出すタイマーを開始する。
timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector:"countDown", userInfo:nil, repeats:true)
}
//タイマーから呼び出されるメソッド
func countDown(){
//カウントを減らす。
count--
//カウントダウン状況をラベルに表示
if(count > 0) {
testLabel.text = "残り\(count)秒です。"
} else {
testLabel.text = "カウントダウン終了"
timer.invalidate()
}
}
//最初からあるメソッド
override func viewDidLoad() {
super.viewDidLoad()
}
}
いつもどおりコードを組み込んで、
![](https://assets.st-note.com/img/1679110215394-2Cqt5NZQpY.png?width=1200)
![](https://assets.st-note.com/img/1679110266149-4MCNScoW8A.png?width=1200)
![](https://assets.st-note.com/img/1679110390219-fjHBDZp599.png?width=1200)
サイト記事の内容は以上✨
ブラッシュアップ
一気に繋いじゃったので、前半とカウントダウンタイマーのビューを分けようかなとも思ったんだけど、、、
ひとつのビューで一気にまとめてしまおう👀
![](https://assets.st-note.com/img/1679110909822-l8MB9bZjPn.png?width=1200)
恒例のAutoLayout〜〜〜
今回は、ひとつのビューにパーツも多いので、VerticalStackViewでまとめよう
![](https://assets.st-note.com/img/1679111308846-mwYuiSgc1s.png?width=1200)
![](https://assets.st-note.com/img/1679111328977-kkpMjKsIdk.png?width=1200)
![](https://assets.st-note.com/img/1679111348022-ity3XzUZ1l.png?width=1200)
![](https://assets.st-note.com/img/1679111393721-aNLXYZ3lGX.png?width=1200)
![](https://assets.st-note.com/img/1679111435504-yMtXm8OBe2.png?width=1200)
![](https://assets.st-note.com/img/1679111478029-1c9IIqzLvd.png?width=1200)
![](https://assets.st-note.com/img/1679111502303-c7C9OKQwku.png?width=1200)
と、ボタンと表示ラベルがこのままだとどれがボタンか区別がつきにくいので〜〜〜
![](https://assets.st-note.com/img/1679111641893-uPUT84rZMP.png?width=1200)
![](https://assets.st-note.com/img/1679111696823-rKTySldHio.png?width=1200)
今回のコード(まとめ)
class DateTimeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var timer:Timer = Timer()
var count:Int = 0
@IBOutlet weak var myDateExpressionLabel: UILabel!
@IBAction func myDatePicker1(_ sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
myDateExpressionLabel.text = formatter.string(from: sender.date)
}
@IBOutlet weak var myDatePicker2: UIDatePicker!
@IBAction func myDatePicker(_ sender: UIDatePicker) {
//何もしない
}
@IBAction func myCountDown(_ sender: UIButton) {
//すでに動いているタイマーは停止する
timer.invalidate()
//カウントダウンする秒数を取得する。
count = Int(myDatePicker2.countDownDuration)
//1秒周期でcountDownメソッドを呼び出すタイマーを開始する。
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector:#selector(self.countDown), userInfo:nil, repeats:true)
}
//タイマーから呼び出されるメソッド
@objc func countDown(){
//カウントを減らす。
count -= 1
//カウントダウン状況をラベルに表示
if(count > 0) {
myDateLabel.text = "残り\(count)秒です。"
} else {
myDateLabel.text = "カウントダウン終了"
timer.invalidate()
}
}
@IBOutlet weak var myDateLabel: UILabel!
}
Apple公式
さて、次回は
をレッツゴする🕺