回答編集履歴

2

頭使おうよ

2020/05/24 13:48

投稿

退会済みユーザー
test CHANGED
@@ -51,3 +51,185 @@
51
51
  }
52
52
 
53
53
  ```
54
+
55
+
56
+
57
+ ---
58
+
59
+
60
+
61
+ ```swift
62
+
63
+
64
+
65
+ import UIKit
66
+
67
+
68
+
69
+ class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
70
+
71
+
72
+
73
+ @IBOutlet weak var upperTextField: UITextField!
74
+
75
+
76
+
77
+ @IBOutlet weak var upperResultLabel: UILabel!
78
+
79
+
80
+
81
+ var upperPickerView = UIPickerView()
82
+
83
+
84
+
85
+ var upperData = ["1", "2", "3","4","5"]
86
+
87
+
88
+
89
+ @IBOutlet weak var lowerTextField: UITextField!
90
+
91
+
92
+
93
+ @IBOutlet weak var lowerResultLabel: UILabel!
94
+
95
+
96
+
97
+ var lowerPickerView = UIPickerView()
98
+
99
+
100
+
101
+ var lowerData = ["A", "B", "C","D","E"]
102
+
103
+
104
+
105
+ override func viewDidLoad() {
106
+
107
+ super.viewDidLoad()
108
+
109
+ createPickerView()
110
+
111
+ }
112
+
113
+
114
+
115
+ func createPickerView() {
116
+
117
+ // upperPickerView
118
+
119
+ upperPickerView.delegate = self
120
+
121
+ upperTextField.inputView = upperPickerView
122
+
123
+ // toolbar
124
+
125
+ let upperToolbar = UIToolbar()
126
+
127
+ upperToolbar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
128
+
129
+ let upperDoneButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(ViewController.donePicker))
130
+
131
+ upperToolbar.setItems([upperDoneButtonItem], animated: true)
132
+
133
+ upperTextField.inputAccessoryView = upperToolbar
134
+
135
+
136
+
137
+ // lowerPickerView
138
+
139
+ lowerPickerView.delegate = self
140
+
141
+ lowerTextField.inputView = lowerPickerView
142
+
143
+ // toolbar
144
+
145
+ let lowerToolbar = UIToolbar()
146
+
147
+ lowerToolbar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
148
+
149
+ let lowerDoneButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(ViewController.donePicker))
150
+
151
+ lowerToolbar.setItems([lowerDoneButtonItem], animated: true)
152
+
153
+ lowerTextField.inputAccessoryView = lowerToolbar
154
+
155
+ }
156
+
157
+
158
+
159
+ @objc func donePicker() {
160
+
161
+ upperTextField.endEditing(true)
162
+
163
+ lowerTextField.endEditing(true)
164
+
165
+ }
166
+
167
+
168
+
169
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
170
+
171
+ upperTextField.endEditing(true)
172
+
173
+ lowerTextField.endEditing(true)
174
+
175
+ }
176
+
177
+
178
+
179
+
180
+
181
+ func numberOfComponents(in pickerView: UIPickerView) -> Int {
182
+
183
+ return 1
184
+
185
+ }
186
+
187
+
188
+
189
+ func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
190
+
191
+ return 5
192
+
193
+ }
194
+
195
+
196
+
197
+ func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
198
+
199
+ if pickerView == upperPickerView {
200
+
201
+ return upperData[row]
202
+
203
+ } else {
204
+
205
+ return lowerData[row]
206
+
207
+ }
208
+
209
+ }
210
+
211
+
212
+
213
+ func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
214
+
215
+ if pickerView == upperPickerView {
216
+
217
+ upperTextField.text = upperData[row]
218
+
219
+ upperResultLabel.text = upperData[row]
220
+
221
+ } else {
222
+
223
+ lowerTextField.text = lowerData[row]
224
+
225
+ lowerResultLabel.text = lowerData[row]
226
+
227
+ }
228
+
229
+ }
230
+
231
+ }
232
+
233
+
234
+
235
+ ```

1

訂正

2020/05/24 13:48

投稿

退会済みユーザー
test CHANGED
@@ -48,10 +48,6 @@
48
48
 
49
49
  }
50
50
 
51
-
52
-
53
-
54
-
55
- コード
51
+ }
56
52
 
57
53
  ```