回答編集履歴
4
修正
test
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
値を渡す時によく使用する代表的なパターンを以下に記載しました、参考にしてみてください。
|
2
2
|
|
3
|
+
※クラスAからクラスBに遷移した前提で書いています。
|
4
|
+
|
3
5
|
|
4
6
|
|
5
7
|
Delegateによる値渡しの例
|
3
修正
test
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
+
値を渡す時によく使用する代表的なパターンを以下に記載しました、参考にしてみてください。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
Delegateによる値渡しの例
|
2
6
|
|
3
7
|
---
|
4
8
|
|
5
9
|
|
6
10
|
|
7
|
-
`ViewController`間の値の渡し方の一つとして`Delegate`を使用するパターンがあります。
|
8
|
-
|
9
|
-
以下にDelegateを使用した値渡しのコードを載せておきます、参考にしてください。
|
10
|
-
|
11
|
-
|
12
|
-
|
13
11
|
```swift
|
14
12
|
|
15
13
|
|
2
修正
test
CHANGED
@@ -210,8 +210,6 @@
|
|
210
210
|
|
211
211
|
|
212
212
|
|
213
|
-
|
214
|
-
|
215
213
|
class TableViewController: UITableViewController {
|
216
214
|
|
217
215
|
|
@@ -257,3 +255,115 @@
|
|
257
255
|
```
|
258
256
|
|
259
257
|
|
258
|
+
|
259
|
+
Notificationによる値渡しの例
|
260
|
+
|
261
|
+
---
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
```swift
|
266
|
+
|
267
|
+
import UIKit
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
class ViewController: UIViewController {
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
@IBOutlet weak var mytext: UITextView!
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
override func viewDidLoad() {
|
280
|
+
|
281
|
+
super.viewDidLoad()
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// 通知を登録
|
286
|
+
|
287
|
+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.update(_:)), name: "TextUpdateNotification", object: nil)
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
deinit {
|
294
|
+
|
295
|
+
NSNotificationCenter.defaultCenter().removeObserver(self)
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
func update(notification: NSNotification?) {
|
302
|
+
|
303
|
+
mytext.text = notification?.object as! String
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
@IBAction func pushNext(sender: AnyObject) {
|
310
|
+
|
311
|
+
// ボタン押下時にTableViewControllerに遷移
|
312
|
+
|
313
|
+
performSegueWithIdentifier("Next", sender: nil)
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
// ------------------------------------------------------------
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
import UIKit
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
class TableViewController: UITableViewController {
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
override func viewDidLoad() {
|
334
|
+
|
335
|
+
super.viewDidLoad()
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
@IBAction func zenbun(sender: AnyObject) {
|
344
|
+
|
345
|
+
// 通知を送信
|
346
|
+
|
347
|
+
NSNotificationCenter.defaultCenter().postNotificationName("TextUpdateNotification", object: "テスト")
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
354
|
+
|
355
|
+
return 0
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
362
|
+
|
363
|
+
return 0
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
```
|
1
修正
test
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
Delegateによる値渡しの例
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
|
6
|
+
|
1
7
|
`ViewController`間の値の渡し方の一つとして`Delegate`を使用するパターンがあります。
|
2
8
|
|
3
9
|
以下にDelegateを使用した値渡しのコードを載せておきます、参考にしてください。
|
@@ -131,3 +137,123 @@
|
|
131
137
|
|
132
138
|
|
133
139
|
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
クロージャーによる値渡しの例
|
144
|
+
|
145
|
+
---
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```swift
|
150
|
+
|
151
|
+
import UIKit
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
class ViewController: UIViewController {
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
@IBOutlet weak var mytext: UITextView!
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
override func viewDidLoad() {
|
164
|
+
|
165
|
+
super.viewDidLoad()
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
@IBAction func pushNext(sender: AnyObject) {
|
174
|
+
|
175
|
+
// ボタン押下時にTableViewControllerに遷移
|
176
|
+
|
177
|
+
performSegueWithIdentifier("Next", sender: nil)
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
if let next = segue.destinationViewController as? TableViewController {
|
188
|
+
|
189
|
+
// クロージャを設定する
|
190
|
+
|
191
|
+
next.closuer = { (text) in
|
192
|
+
|
193
|
+
self.mytext.text = text
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
// ------------------------------------------------------------
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
import UIKit
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
class TableViewController: UITableViewController {
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
var closuer: (String -> ())!
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
override func viewDidLoad() {
|
224
|
+
|
225
|
+
super.viewDidLoad()
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
@IBAction func zenbun(sender: AnyObject) {
|
234
|
+
|
235
|
+
closuer("テスト")
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
242
|
+
|
243
|
+
return 0
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
250
|
+
|
251
|
+
return 0
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
```
|
258
|
+
|
259
|
+
|