teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを追記しました

2020/01/05 10:18

投稿

tubajuro16
tubajuro16

スコア12

title CHANGED
File without changes
body CHANGED
@@ -12,4 +12,151 @@
12
12
  ①Buttonから画面遷移先へドラッグ&ドロップ
13
13
  ②画面遷移前のViewControllerでButtonのOutlet及びActionを設定。また、Action内に
14
14
  self.performSegue(withIdentifier: "画面遷移先のIdentifier", sender: nil)
15
- を入力及びsegueのIdentifierを設定
15
+ を入力及びsegueのIdentifierを設定
16
+
17
+
18
+ ```Swift
19
+ // TestViewController.swift
20
+
21
+ import UIKit
22
+ import Firebase
23
+
24
+ class TestViewController: UIViewController,UITextFieldDelegate {
25
+
26
+ @IBOutlet weak var sampleLabel: UILabel!
27
+
28
+ @IBOutlet weak var inputTextField: UITextField!
29
+
30
+ @IBOutlet weak var sendButton: UIButton!
31
+
32
+ var testArray = [Sample]()
33
+
34
+ let screenSize = UIScreen.main.bounds.size
35
+
36
+
37
+ override func viewDidLoad() {
38
+ super.viewDidLoad()
39
+
40
+ inputTextField.delegate = self
41
+
42
+ NotificationCenter.default.addObserver(self, selector: #selector(TestViewController.keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
43
+
44
+ NotificationCenter.default.addObserver(self, selector: #selector(TestViewController.keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
45
+
46
+ }
47
+
48
+ //キーボードを開く
49
+ @objc func keyboardWillShow(_ notification:NSNotification){
50
+
51
+ let keyboardHeight = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as Any) as AnyObject).cgRectValue.height
52
+
53
+ inputTextField.frame.origin.y = screenSize.height - keyboardHeight - inputTextField.frame.height
54
+
55
+
56
+ }
57
+
58
+ //キーボードを閉じる
59
+ @objc func keyboardWillHide(_ notification:NSNotification){
60
+
61
+ inputTextField.frame.origin.y = screenSize.height - inputTextField.frame.height
62
+
63
+ guard let rect = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
64
+ let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else{return}
65
+ UIView.animate(withDuration: duration) {
66
+
67
+
68
+ let transform = CGAffineTransform(translationX: 0, y: 0)
69
+
70
+ self.view.transform = transform
71
+
72
+ }
73
+ }
74
+
75
+ //TextFieldを押した時
76
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
77
+
78
+ inputTextField.resignFirstResponder()
79
+
80
+ }
81
+
82
+ //TextFieldでEnterを押した時
83
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
84
+
85
+ textField.resignFirstResponder()
86
+ return true
87
+
88
+ }
89
+
90
+
91
+
92
+ //sendを押した時にfirebaseへ保存
93
+ @IBAction func sendAction(_ sender: Any) {
94
+
95
+ inputTextField.endEditing(true)
96
+ inputTextField.isEnabled = false
97
+ sendButton.isEnabled = false
98
+
99
+ let testDB = Database.database().reference().child("tests")
100
+
101
+ let inputInfo = ["sender":Auth.auth().currentUser?.email,"sample":sampleLabel.text!,"input":inputTextField.text!]
102
+
103
+ testDB.childByAutoId().setValue(inputInfo) { (error, result) in
104
+
105
+ if error != nil{
106
+ print(error)
107
+ }else{
108
+ print("送信完了しました!")
109
+ self.inputTextField.isEnabled = true
110
+ self.sendButton.isEnabled = true
111
+ self.inputTextField.text = ""
112
+ }
113
+
114
+ }
115
+
116
+ }
117
+
118
+ //Firebaseから取り出す
119
+ func fetchTestData(){
120
+
121
+ let fetchDataRef = Database.database().reference().child("tests")
122
+
123
+ fetchDataRef.observe(.childAdded) { (snapShot) in
124
+ let snapShotData = snapShot.value as AnyObject
125
+ let input = snapShotData.value(forKey: "input")
126
+ let sample = snapShotData.value(forKey: "sample")
127
+ let sender = snapShotData.value(forKey: "sender")
128
+
129
+ let samples = Sample()
130
+ samples.input = input as! String
131
+ samples.sample = sample as! String
132
+ samples.sender = sender as! String
133
+ self.testArray.append(samples)
134
+ //リロードが必要
135
+ }
136
+
137
+ }
138
+
139
+ // inputデータのみを取得したい(未完成)
140
+ // func fetchTestDataInLabel(){
141
+ //
142
+ // let fetchDataRef = Database.database().reference().child("tests")
143
+ //
144
+ // fetchDataRef.observe(.childChanged) { (snapShot) in
145
+ // let snapShotData = snapShot.value as AnyObject
146
+ // let sample = snapShotData.value(forKey: "sample")
147
+ // }
148
+ // }
149
+
150
+ /*
151
+ // MARK: - Navigation
152
+
153
+ // In a storyboard-based application, you will often want to do a little preparation before navigation
154
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
155
+ // Get the new view controller using segue.destination.
156
+ // Pass the selected object to the new view controller.
157
+ }
158
+ */
159
+
160
+ }
161
+
162
+ ```