質問編集履歴

1

コードを追記しました

2020/01/05 10:18

投稿

tubajuro16
tubajuro16

スコア12

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,297 @@
27
27
  self.performSegue(withIdentifier: "画面遷移先のIdentifier", sender: nil)
28
28
 
29
29
  を入力及びsegueのIdentifierを設定
30
+
31
+
32
+
33
+
34
+
35
+ ```Swift
36
+
37
+ // TestViewController.swift
38
+
39
+
40
+
41
+ import UIKit
42
+
43
+ import Firebase
44
+
45
+
46
+
47
+ class TestViewController: UIViewController,UITextFieldDelegate {
48
+
49
+
50
+
51
+ @IBOutlet weak var sampleLabel: UILabel!
52
+
53
+
54
+
55
+ @IBOutlet weak var inputTextField: UITextField!
56
+
57
+
58
+
59
+ @IBOutlet weak var sendButton: UIButton!
60
+
61
+
62
+
63
+ var testArray = [Sample]()
64
+
65
+
66
+
67
+ let screenSize = UIScreen.main.bounds.size
68
+
69
+
70
+
71
+
72
+
73
+ override func viewDidLoad() {
74
+
75
+ super.viewDidLoad()
76
+
77
+
78
+
79
+ inputTextField.delegate = self
80
+
81
+
82
+
83
+ NotificationCenter.default.addObserver(self, selector: #selector(TestViewController.keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
84
+
85
+
86
+
87
+ NotificationCenter.default.addObserver(self, selector: #selector(TestViewController.keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
88
+
89
+
90
+
91
+ }
92
+
93
+
94
+
95
+ //キーボードを開く
96
+
97
+ @objc func keyboardWillShow(_ notification:NSNotification){
98
+
99
+
100
+
101
+ let keyboardHeight = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as Any) as AnyObject).cgRectValue.height
102
+
103
+
104
+
105
+ inputTextField.frame.origin.y = screenSize.height - keyboardHeight - inputTextField.frame.height
106
+
107
+
108
+
109
+
110
+
111
+ }
112
+
113
+
114
+
115
+ //キーボードを閉じる
116
+
117
+ @objc func keyboardWillHide(_ notification:NSNotification){
118
+
119
+
120
+
121
+ inputTextField.frame.origin.y = screenSize.height - inputTextField.frame.height
122
+
123
+
124
+
125
+ guard let rect = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
126
+
127
+ let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else{return}
128
+
129
+ UIView.animate(withDuration: duration) {
130
+
131
+
132
+
133
+
134
+
135
+ let transform = CGAffineTransform(translationX: 0, y: 0)
136
+
137
+
138
+
139
+ self.view.transform = transform
140
+
141
+
142
+
143
+ }
144
+
145
+ }
146
+
147
+
148
+
149
+ //TextFieldを押した時
150
+
151
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
152
+
153
+
154
+
155
+ inputTextField.resignFirstResponder()
156
+
157
+
158
+
159
+ }
160
+
161
+
162
+
163
+ //TextFieldでEnterを押した時
164
+
165
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
166
+
167
+
168
+
169
+ textField.resignFirstResponder()
170
+
171
+ return true
172
+
173
+
174
+
175
+ }
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+ //sendを押した時にfirebaseへ保存
184
+
185
+ @IBAction func sendAction(_ sender: Any) {
186
+
187
+
188
+
189
+ inputTextField.endEditing(true)
190
+
191
+ inputTextField.isEnabled = false
192
+
193
+ sendButton.isEnabled = false
194
+
195
+
196
+
197
+ let testDB = Database.database().reference().child("tests")
198
+
199
+
200
+
201
+ let inputInfo = ["sender":Auth.auth().currentUser?.email,"sample":sampleLabel.text!,"input":inputTextField.text!]
202
+
203
+
204
+
205
+ testDB.childByAutoId().setValue(inputInfo) { (error, result) in
206
+
207
+
208
+
209
+ if error != nil{
210
+
211
+ print(error)
212
+
213
+ }else{
214
+
215
+ print("送信完了しました!")
216
+
217
+ self.inputTextField.isEnabled = true
218
+
219
+ self.sendButton.isEnabled = true
220
+
221
+ self.inputTextField.text = ""
222
+
223
+ }
224
+
225
+
226
+
227
+ }
228
+
229
+
230
+
231
+ }
232
+
233
+
234
+
235
+ //Firebaseから取り出す
236
+
237
+ func fetchTestData(){
238
+
239
+
240
+
241
+ let fetchDataRef = Database.database().reference().child("tests")
242
+
243
+
244
+
245
+ fetchDataRef.observe(.childAdded) { (snapShot) in
246
+
247
+ let snapShotData = snapShot.value as AnyObject
248
+
249
+ let input = snapShotData.value(forKey: "input")
250
+
251
+ let sample = snapShotData.value(forKey: "sample")
252
+
253
+ let sender = snapShotData.value(forKey: "sender")
254
+
255
+
256
+
257
+ let samples = Sample()
258
+
259
+ samples.input = input as! String
260
+
261
+ samples.sample = sample as! String
262
+
263
+ samples.sender = sender as! String
264
+
265
+ self.testArray.append(samples)
266
+
267
+ //リロードが必要
268
+
269
+ }
270
+
271
+
272
+
273
+ }
274
+
275
+
276
+
277
+ // inputデータのみを取得したい(未完成)
278
+
279
+ // func fetchTestDataInLabel(){
280
+
281
+ //
282
+
283
+ // let fetchDataRef = Database.database().reference().child("tests")
284
+
285
+ //
286
+
287
+ // fetchDataRef.observe(.childChanged) { (snapShot) in
288
+
289
+ // let snapShotData = snapShot.value as AnyObject
290
+
291
+ // let sample = snapShotData.value(forKey: "sample")
292
+
293
+ // }
294
+
295
+ // }
296
+
297
+
298
+
299
+ /*
300
+
301
+ // MARK: - Navigation
302
+
303
+
304
+
305
+ // In a storyboard-based application, you will often want to do a little preparation before navigation
306
+
307
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
308
+
309
+ // Get the new view controller using segue.destination.
310
+
311
+ // Pass the selected object to the new view controller.
312
+
313
+ }
314
+
315
+ */
316
+
317
+
318
+
319
+ }
320
+
321
+
322
+
323
+ ```