回答編集履歴
1
サンプルコードの追加
test
CHANGED
@@ -5,3 +5,357 @@
|
|
5
5
|
このように設定すれば日付と時間、年全て入力できますが。
|
6
6
|
|
7
7
|
どうしても`wheels`で実装したいと言うことでしょうか?その場合は標準では用意されていないみたいなので自分で作成する必要がありそうです。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
[追記]
|
12
|
+
|
13
|
+
自分が以前実装した時のコードです。このまま使えないとは思いますが参考までに・・・。
|
14
|
+
|
15
|
+
```Swift
|
16
|
+
|
17
|
+
import UIKit
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
class ViewController: UIViewController {
|
24
|
+
|
25
|
+
// textFieldなのかわかりませんが必要であれば変えてください
|
26
|
+
|
27
|
+
// それとoutletを繋げてください
|
28
|
+
|
29
|
+
@IBOutlet weak var textField: UITextField!
|
30
|
+
|
31
|
+
var picker: UIPickerView!
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
var toolbar: UIToolbar = {
|
36
|
+
|
37
|
+
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 35))
|
38
|
+
|
39
|
+
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
|
40
|
+
|
41
|
+
toolbar.setItems([doneItem], animated: true)
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
return toolbar
|
46
|
+
|
47
|
+
}()
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
private let yearList: [Int] = Array(2000...2100)
|
52
|
+
|
53
|
+
private let monthList: [Int] = Array(1...12)
|
54
|
+
|
55
|
+
private let dayList: [Int] = Array(1...31)
|
56
|
+
|
57
|
+
private let hourList: [Int] = Array(0...23)
|
58
|
+
|
59
|
+
private let minuteList: [Int] = Array(0...59)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
var dateFormatter: DateFormatter = {
|
64
|
+
|
65
|
+
let f = DateFormatter()
|
66
|
+
|
67
|
+
// 表示用に使うフォーマット
|
68
|
+
|
69
|
+
// 必要であれば変えてください
|
70
|
+
|
71
|
+
f.dateFormat = "yyyy/MM/dd hh:mm"
|
72
|
+
|
73
|
+
return f
|
74
|
+
|
75
|
+
}()
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
override func viewDidLoad() {
|
80
|
+
|
81
|
+
super.viewDidLoad()
|
82
|
+
|
83
|
+
picker = UIPickerView()
|
84
|
+
|
85
|
+
picker.delegate = self
|
86
|
+
|
87
|
+
picker.dataSource = self
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
textField.inputView = picker
|
94
|
+
|
95
|
+
textField.inputAccessoryView = toolbar
|
96
|
+
|
97
|
+
setTodayValue()
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
@objc func done() {
|
104
|
+
|
105
|
+
textField.endEditing(true)
|
106
|
+
|
107
|
+
updateTextField()
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
114
|
+
|
115
|
+
super.touchesBegan(touches, with: event)
|
116
|
+
|
117
|
+
textField.endEditing(true)
|
118
|
+
|
119
|
+
updateTextField()
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
func setTodayValue() {
|
126
|
+
|
127
|
+
let date = Date()
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
let f0 = DateFormatter()
|
132
|
+
|
133
|
+
f0.dateFormat = "yyyy"
|
134
|
+
|
135
|
+
if let int = Int(f0.string(from: date)), let row = yearList.firstIndex(of: int) {
|
136
|
+
|
137
|
+
picker.selectRow(row, inComponent: 0, animated: false)
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
let f1 = DateFormatter()
|
144
|
+
|
145
|
+
f1.dateFormat = "M"
|
146
|
+
|
147
|
+
if let int = Int(f1.string(from: date)), let row = monthList.firstIndex(of: int) {
|
148
|
+
|
149
|
+
picker.selectRow(row, inComponent: 1, animated: false)
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
let f2 = DateFormatter()
|
156
|
+
|
157
|
+
f2.dateFormat = "d"
|
158
|
+
|
159
|
+
if let int = Int(f2.string(from: date)), let row = dayList.firstIndex(of: int) {
|
160
|
+
|
161
|
+
picker.selectRow(row, inComponent: 2, animated: false)
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
let f3 = DateFormatter()
|
168
|
+
|
169
|
+
f3.dateFormat = "H"
|
170
|
+
|
171
|
+
if let int = Int(f3.string(from: date)), let row = hourList.firstIndex(of: int) {
|
172
|
+
|
173
|
+
picker.selectRow(row, inComponent: 3, animated: false)
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
let f4 = DateFormatter()
|
180
|
+
|
181
|
+
f4.dateFormat = "m"
|
182
|
+
|
183
|
+
if let int = Int(f4.string(from: date)), let row = minuteList.firstIndex(of: int) {
|
184
|
+
|
185
|
+
picker.selectRow(row, inComponent: 4, animated: false)
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
updateTextField()
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
func updateTextField() {
|
196
|
+
|
197
|
+
if let date = fetchDateFromPicker() {
|
198
|
+
|
199
|
+
textField.text = dateFormatter.string(from: date)
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
func fetchDateFromPicker() -> Date? {
|
208
|
+
|
209
|
+
let selectedYear = yearList[picker.selectedRow(inComponent: 0)]
|
210
|
+
|
211
|
+
let selectedMonth = String(monthList[picker.selectedRow(inComponent: 1)])
|
212
|
+
|
213
|
+
let selectedDay = String(dayList[picker.selectedRow(inComponent: 2)])
|
214
|
+
|
215
|
+
let selectedHour = String(hourList[picker.selectedRow(inComponent: 3)])
|
216
|
+
|
217
|
+
let selectedMinute = String(minuteList[picker.selectedRow(inComponent: 4)])
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
let string = "(selectedYear)/(selectedMonth)/(selectedDay) (selectedHour):(selectedMinute)"
|
222
|
+
|
223
|
+
let f = DateFormatter()
|
224
|
+
|
225
|
+
f.dateFormat = "yyyy/M/d H:m"
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
return f.date(from: string)
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
242
|
+
|
243
|
+
return 5
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
250
|
+
|
251
|
+
switch component {
|
252
|
+
|
253
|
+
case 0:
|
254
|
+
|
255
|
+
return yearList.count
|
256
|
+
|
257
|
+
case 1:
|
258
|
+
|
259
|
+
return monthList.count
|
260
|
+
|
261
|
+
case 2:
|
262
|
+
|
263
|
+
return dayList.count
|
264
|
+
|
265
|
+
case 3:
|
266
|
+
|
267
|
+
return hourList.count
|
268
|
+
|
269
|
+
case 4 :
|
270
|
+
|
271
|
+
return minuteList.count
|
272
|
+
|
273
|
+
default:
|
274
|
+
|
275
|
+
return 0
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
284
|
+
|
285
|
+
switch component {
|
286
|
+
|
287
|
+
case 0:
|
288
|
+
|
289
|
+
return String(yearList[row])
|
290
|
+
|
291
|
+
case 1:
|
292
|
+
|
293
|
+
return String(format: "%02d", monthList[row])
|
294
|
+
|
295
|
+
case 2:
|
296
|
+
|
297
|
+
return String(format: "%02d", dayList[row])
|
298
|
+
|
299
|
+
case 3:
|
300
|
+
|
301
|
+
return String(format: "%02d", hourList[row])
|
302
|
+
|
303
|
+
case 4 :
|
304
|
+
|
305
|
+
return String(format: "%02d", minuteList[row])
|
306
|
+
|
307
|
+
default:
|
308
|
+
|
309
|
+
return nil
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
318
|
+
|
319
|
+
updateTextField()
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
|
326
|
+
|
327
|
+
switch component {
|
328
|
+
|
329
|
+
case 0:
|
330
|
+
|
331
|
+
return 100
|
332
|
+
|
333
|
+
case 1:
|
334
|
+
|
335
|
+
return 40
|
336
|
+
|
337
|
+
case 2:
|
338
|
+
|
339
|
+
return 40
|
340
|
+
|
341
|
+
case 3:
|
342
|
+
|
343
|
+
return 40
|
344
|
+
|
345
|
+
case 4 :
|
346
|
+
|
347
|
+
return 40
|
348
|
+
|
349
|
+
default:
|
350
|
+
|
351
|
+
return 0
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
```
|