質問編集履歴

2

変更

2018/06/23 02:27

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  //textFieldのあるモーダルビュー(ModalViewController)を追記しました。
38
38
 
39
-
39
+ [参考にしたドキュメントです](https://picolica.com/2017/04/04/swift3-get-from-modalview/)
40
40
 
41
41
  ```ここに言語を入力
42
42
 

1

変更

2018/06/23 02:27

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,219 @@
29
29
 
30
30
 
31
31
  よろしくお願いします。
32
+
33
+
34
+
35
+
36
+
37
+ //textFieldのあるモーダルビュー(ModalViewController)を追記しました。
38
+
39
+
40
+
41
+ ```ここに言語を入力
42
+
43
+
44
+
45
+
46
+
47
+ import UIKit
48
+
49
+
50
+
51
+ class ModalViewController: UIViewController, UITextFieldDelegate {
52
+
53
+
54
+
55
+ var changeButton = UIButton()
56
+
57
+ var textField = UITextField()
58
+
59
+
60
+
61
+ override func viewDidLoad() {
62
+
63
+ super.viewDidLoad()
64
+
65
+ set()
66
+
67
+ }
68
+
69
+
70
+
71
+
72
+
73
+ func set() {
74
+
75
+ view.backgroundColor = .gray
76
+
77
+
78
+
79
+ let cancelButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
80
+
81
+ cancelButton.center = CGPoint(x: view.center.x - 100, y: view.center.y)
82
+
83
+ cancelButton.setTitle("キャンセル", for: .normal)
84
+
85
+ cancelButton.setTitleColor(UIColor.black, for: .normal)
86
+
87
+ cancelButton.addTarget(self, action: #selector(pushDismiss(sender:)), for: .touchUpInside)
88
+
89
+ view.addSubview(cancelButton)
90
+
91
+
92
+
93
+
94
+
95
+ //
96
+
97
+
98
+
99
+
100
+
101
+ changeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
102
+
103
+ changeButton.center = CGPoint(x: view.center.x + 100, y: view.center.y)
104
+
105
+ changeButton.setTitle("変更", for: .normal)
106
+
107
+ changeButton.setTitleColor(UIColor.black, for: .normal)
108
+
109
+ changeButton.addTarget(self, action: #selector(pushChange(sender:)), for: .touchUpInside)
110
+
111
+
112
+
113
+ changeButton.isEnabled = false
114
+
115
+ view.addSubview(changeButton)
116
+
117
+
118
+
119
+ textField.delegate = self
120
+
121
+ textField.text = ""
122
+
123
+
124
+
125
+ textField = UITextField(frame: CGRect(x: 0, y: 0, width: view.bounds.width - 20, height: 30))
126
+
127
+ textField.center = CGPoint(x: view.center.x, y: view.center.y + 50)
128
+
129
+ textField.placeholder = "入力してね"
130
+
131
+ textField.backgroundColor = .white
132
+
133
+ view.addSubview(textField)
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ let center = NotificationCenter.default
144
+
145
+
146
+
147
+ center.addObserver(・省略してます)
148
+
149
+ }
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+ @objc func textFieldDidChange(notification: NSNotification) {
158
+
159
+
160
+
161
+ if textField.text == "" {
162
+
163
+ changeButton.isEnabled = false
164
+
165
+ } else {
166
+
167
+ changeButton.isEnabled = true
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
182
+
183
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
184
+
185
+
186
+
187
+ self.view.resignFirstResponder()
188
+
189
+ }
190
+
191
+ return true
192
+
193
+
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+ //変更
204
+
205
+
206
+
207
+ @objc func pushChange(sender:UIButton) {
208
+
209
+
210
+
211
+ textField.resignFirstResponder()
212
+
213
+
214
+
215
+ let originVc = presentingViewController as! HomeViewController
216
+
217
+ originVc.textFromModal = textField.text!
218
+
219
+
220
+
221
+ self.dismiss(animated: true, completion: nil)
222
+
223
+ }
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+ //キャンセル
232
+
233
+ @objc func pushDismiss(sender:UIButton) {
234
+
235
+ self.dismiss(animated: true, completion: nil)
236
+
237
+ }
238
+
239
+
240
+
241
+ }
242
+
243
+
244
+
245
+
246
+
247
+ ```