質問編集履歴
2
shuusei
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
①同じビュー内に違う値を持つPickerViewを2つ表示する方法
|
26
26
|
|
27
|
-
|
27
|
+
|
28
28
|
|
29
29
|
|
30
30
|
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -149,3 +149,149 @@
|
|
149
149
|
|
150
150
|
|
151
151
|
```
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
追記です。
|
156
|
+
|
157
|
+
```swift
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
import UIKit
|
164
|
+
|
165
|
+
class testViewController: UIViewController, UITextFieldDelegate{
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
var toolBar:UIToolbar!
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
@IBOutlet weak var wakeTimeTextField: UITextField!
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
@IBOutlet weak var textfield2: UITextField!
|
180
|
+
|
181
|
+
override func viewDidLoad() {
|
182
|
+
|
183
|
+
super.viewDidLoad()
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
wakeTimeTextField.tag = 1
|
188
|
+
|
189
|
+
wakeTimeTextField.delegate = self
|
190
|
+
|
191
|
+
setupToolbar()
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
textfield2.tag = 2
|
196
|
+
|
197
|
+
textfield2.delegate = self
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
func setupToolbar() {
|
202
|
+
|
203
|
+
//datepicker上のtoolbarのdoneボタン
|
204
|
+
|
205
|
+
toolBar = UIToolbar()
|
206
|
+
|
207
|
+
toolBar.sizeToFit()
|
208
|
+
|
209
|
+
let toolBarBtn = UIBarButtonItem(title: "DONE", style: .plain, target: self, action: #selector(doneBtn))
|
210
|
+
|
211
|
+
toolBar.items = [toolBarBtn]
|
212
|
+
|
213
|
+
wakeTimeTextField.inputAccessoryView = toolBar
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
func textFieldDidBeginEditing(_ textField: UITextField) {
|
220
|
+
|
221
|
+
if wakeTimeTextField.tag == 1{
|
222
|
+
|
223
|
+
let datePickerView:UIDatePicker = UIDatePicker()
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
datePickerView.tag = 1//追加
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
datePickerView.datePickerMode = UIDatePicker.Mode.date
|
232
|
+
|
233
|
+
textField.inputView = datePickerView
|
234
|
+
|
235
|
+
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(sender:)), for: UIControl.Event.valueChanged)}else{
|
236
|
+
|
237
|
+
let datePickerView:UIDatePicker = UIDatePicker()
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
datePickerView.tag = 2//追加
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
datePickerView.datePickerMode = UIDatePicker.Mode.time
|
248
|
+
|
249
|
+
textField.inputView = datePickerView
|
250
|
+
|
251
|
+
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(sender:)), for: UIControl.Event.valueChanged)}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
//datepickerが選択されたらtextfieldに表示
|
258
|
+
|
259
|
+
@objc func datePickerValueChanged(sender:UIDatePicker) {
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if sender.tag == 1{//変更
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
let dateFormatter = DateFormatter()
|
268
|
+
|
269
|
+
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "yMMMdE", options: 0, locale: Locale(identifier: "ja_JP"))
|
270
|
+
|
271
|
+
wakeTimeTextField.text = dateFormatter.string(from: sender.date)}else
|
272
|
+
|
273
|
+
{
|
274
|
+
|
275
|
+
let dateFormatter = DateFormatter()
|
276
|
+
|
277
|
+
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "H:mm", options: 0, locale: Locale(identifier: "ja_JP"))
|
278
|
+
|
279
|
+
textfield2.text = dateFormatter.string(from: sender.date)}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
//toolbarのdoneボタン
|
286
|
+
|
287
|
+
@objc func doneBtn(){
|
288
|
+
|
289
|
+
wakeTimeTextField.resignFirstResponder()
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
```
|