質問編集履歴
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,9 @@
|
|
16
16
|
|
17
17
|
以下コードの写しになります。
|
18
18
|
|
19
|
+
```
|
20
|
+
|
19
|
-
|
21
|
+
import UIKit
|
20
22
|
|
21
23
|
|
22
24
|
|
@@ -110,6 +112,4 @@
|
|
110
112
|
|
111
113
|
}
|
112
114
|
|
113
|
-
コード
|
114
|
-
|
115
115
|
```
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
以下コードの写しになります。
|
18
18
|
|
19
|
-
import UIKit
|
19
|
+
```import UIKit
|
20
20
|
|
21
21
|
|
22
22
|
|
@@ -24,98 +24,92 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
|
27
|
+
//UITextfieldに紐づいてる
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
@IBOutlet weak var dateField: UITextField!
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
//UIDatePickerを定義するための変数
|
38
|
-
|
39
|
-
var datePicker: UIDatePicker = UIDatePicker()
|
40
28
|
|
41
29
|
|
42
30
|
|
43
|
-
|
44
|
-
|
45
|
-
override func viewDidLoad() {
|
46
|
-
|
47
|
-
super.viewDidLoad()
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
// ピッカー設定
|
52
|
-
|
53
|
-
|
31
|
+
@IBOutlet weak var dateField: UITextField!
|
54
|
-
|
55
|
-
datePicker.timeZone = NSTimeZone.local
|
56
|
-
|
57
|
-
datePicker.locale = Locale.current
|
58
|
-
|
59
|
-
dateField.inputView = datePicker
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
// 決定バーの生成
|
66
|
-
|
67
|
-
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
|
68
|
-
|
69
|
-
let spacelItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
|
70
|
-
|
71
|
-
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
|
72
|
-
|
73
|
-
toolbar.setItems([spacelItem, doneItem], animated: true)
|
74
32
|
|
75
33
|
|
76
34
|
|
77
|
-
|
35
|
+
//UIDatePickerを定義するための変数
|
78
36
|
|
79
|
-
|
37
|
+
var datePicker: UIDatePicker = UIDatePicker()
|
80
|
-
|
81
|
-
dateField.inputAccessoryView = toolbar
|
82
|
-
|
83
|
-
}
|
84
38
|
|
85
39
|
|
86
40
|
|
87
|
-
|
41
|
+
override func viewDidLoad() {
|
88
42
|
|
89
|
-
|
43
|
+
super.viewDidLoad()
|
90
|
-
|
91
|
-
dateField.endEditing(true)
|
92
44
|
|
93
45
|
|
94
46
|
|
95
|
-
|
47
|
+
// ピッカー設定
|
96
48
|
|
49
|
+
datePicker.datePickerMode = UIDatePicker.Mode.date
|
50
|
+
|
51
|
+
datePicker.timeZone = NSTimeZone.local
|
52
|
+
|
53
|
+
datePicker.locale = Locale.current
|
54
|
+
|
97
|
-
|
55
|
+
dateField.inputView = datePicker
|
98
56
|
|
99
57
|
|
100
58
|
|
101
|
-
|
59
|
+
// 決定バーの生成
|
102
60
|
|
61
|
+
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
|
62
|
+
|
63
|
+
let spacelItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
|
64
|
+
|
65
|
+
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
|
66
|
+
|
103
|
-
|
67
|
+
toolbar.setItems([spacelItem, doneItem], animated: true)
|
104
68
|
|
105
69
|
|
106
70
|
|
107
|
-
|
71
|
+
// インプットビュー設定(紐づいているUITextfieldへ代入)
|
108
72
|
|
109
|
-
|
73
|
+
dateField.inputView = datePicker
|
110
74
|
|
111
|
-
|
75
|
+
dateField.inputAccessoryView = toolbar
|
76
|
+
|
77
|
+
}
|
112
78
|
|
113
79
|
|
114
80
|
|
81
|
+
// UIDatePickerのDoneを押したら発火
|
82
|
+
|
83
|
+
@objc func done() {
|
84
|
+
|
85
|
+
dateField.endEditing(true)
|
115
86
|
|
116
87
|
|
117
88
|
|
89
|
+
// 日付のフォーマット
|
118
90
|
|
119
|
-
|
91
|
+
let formatter = DateFormatter()
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
//"yyyy年MM月dd日"を"yyyy/MM/dd"したりして出力の仕方を好きに変更できるよ
|
96
|
+
|
97
|
+
formatter.dateFormat = "yyyy年MM月dd日"
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
//(from: datePicker.date))を指定してあげることで
|
102
|
+
|
103
|
+
//datePickerで指定した日付が表示される
|
104
|
+
|
105
|
+
dateField.text = "(formatter.string(from: datePicker.date))"
|
106
|
+
|
107
|
+
|
120
108
|
|
121
109
|
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
コード
|
114
|
+
|
115
|
+
```
|