質問編集履歴

2

追記

2015/09/26 11:09

投稿

Kesth
Kesth

スコア83

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- 現在、**NavigationController+TableView**を組み合わせた
17
+ 現在、**NavigationController+TableViewController**を組み合わせた
18
18
 
19
19
  設定画面のようなものを実装しており、そこでの値の受け渡しの
20
20
 
@@ -22,80 +22,468 @@
22
22
 
23
23
 
24
24
 
25
- イメージとしては、下記の様な形で値の受け渡しを行いたいと
26
-
27
- 考えています。
28
-
29
-
30
-
31
-
32
-
33
- ▽**ProfileTableView(遷移元)**
34
-
35
- 遷移入力された値(文章)を表示る**UILabel**を保持している
36
-
37
-
38
-
39
- 画面遷移↓↑**TextField**に入力した値をUILabelに渡
40
-
41
-
42
-
43
- ▽**ProfileEditView(遷移先)**
44
-
45
- →プロフィールを編集するための**UITextField**を保持している
46
-
47
-
48
-
49
-
50
-
51
- 上で、
52
-
53
-
54
-
55
- ①**ProfileEditView**において、**UITextField**で入力された値を
56
-
57
- ProfileTableViewに渡すための**Protocol**と**Delegate**を実装
58
-
59
- ②①の値を取得するために**delegate**を実装し
60
-
61
- 受け取った値を**UILabel**内の**text**に格納
62
-
63
-
64
-
65
- といったコードを書いているのですが、①の段階でエラーを吐いてしまい
66
-
67
- 実装することができません。
68
-
69
-
70
-
71
- ソースコードが間違っているのではないかと思い、色々見てみましたが
72
-
73
- 特に間違いはなさそうで、**NavigationController**ではなく**Modal**による
74
-
75
- 画面遷移の場合は上記の方法で値の受け渡しがうまくいきました。
76
-
77
-
78
-
79
-
80
-
81
- 以上のことから、**NavigationController**における画面遷移では**Protocol**と**Delegate**を
82
-
83
- 使用した値の受け渡しはできないのかなと考えましたが
84
-
85
- いかがでしょうか?
86
-
87
-
88
-
89
- ただいま出先のため、ソースコードが貼れず申し訳ないです。
90
-
91
-
92
-
93
- もしできる場合や他にいい方法があれば教えて頂ければと思います。
94
-
95
-
96
-
97
- 【参考にしたページ】
98
-
99
- 自分が書いているソースも下記のような形に近いです
100
-
101
- [http://tech.eversense.co.jp/153](http://tech.eversense.co.jp/153)
25
+
26
+
27
+ 【追記】
28
+
29
+ 現在自分が書いているソースコードを添付します。
30
+
31
+
32
+
33
+
34
+
35
+ 遷移です
36
+
37
+ ProfileSettingViewControllerのProfileTextLabelのTextに遷移先から
38
+
39
+ 渡された値をセットしたいで
40
+
41
+ ```Swift
42
+
43
+ import UIKit
44
+
45
+
46
+
47
+ class ProfileSettingViewController: UIViewController,UITableViewDelegate,updateProfileDelegate,UITableViewDataSource,UINavigationControllerDelegate{
48
+
49
+
50
+
51
+ //遷移先VC
52
+
53
+ let profileEditView = ProfileEditViewController()
54
+
55
+
56
+
57
+ var profileTableView:UITableView!
58
+
59
+
60
+
61
+ var profileTextLabel: UILabel!
62
+
63
+
64
+
65
+ override func viewDidLoad() {
66
+
67
+ super.viewDidLoad()
68
+
69
+
70
+
71
+ self.view.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0)
72
+
73
+
74
+
75
+ self.navigationItem.title = "プロフィール編集"
76
+
77
+
78
+
79
+ let displayWidth: CGFloat = self.view.frame.width
80
+
81
+
82
+
83
+ profileTableView = UITableView(
84
+
85
+ frame: CGRect(
86
+
87
+ x: 0,
88
+
89
+ y: 135,
90
+
91
+ width: displayWidth,
92
+
93
+ height: 210
94
+
95
+ ),style: .Grouped
96
+
97
+ )
98
+
99
+
100
+
101
+ profileTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "profileCell")
102
+
103
+ profileTableView.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0)
104
+
105
+ profileTableView.scrollEnabled = false
106
+
107
+ profileTableView.dataSource = self
108
+
109
+ profileTableView.delegate = self
110
+
111
+
112
+
113
+ self.view.addSubview(profileTableView)
114
+
115
+
116
+
117
+ self.profileEditView.delegate = self
118
+
119
+
120
+
121
+ }
122
+
123
+
124
+
125
+ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
126
+
127
+ return 1
128
+
129
+ }
130
+
131
+
132
+
133
+
134
+
135
+ /*
136
+
137
+ return the number of section
138
+
139
+ */
140
+
141
+ func numberOfSectionsInTableView(tableView: UITableView) -> Int {
142
+
143
+ return 1
144
+
145
+ }
146
+
147
+
148
+
149
+
150
+
151
+ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
152
+
153
+
154
+
155
+ let cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath)
156
+
157
+
158
+
159
+ if indexPath.section == 0{
160
+
161
+
162
+
163
+ case 0:
164
+
165
+ cell.textLabel?.text = "プロフィール"
166
+
167
+ cell.textLabel?.font = UIFont(name:"Helvetica Neue", size: 14.5)
168
+
169
+ cell.textLabel?.textColor = UIColor(red: 62/255, green: 61/255, blue: 61/255, alpha: 0.9)
170
+
171
+ cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
172
+
173
+
174
+
175
+ profileTextLabel = UILabel()
176
+
177
+ profileTextLabel.text = "はじめまして。よろしくお願いします。"
178
+
179
+ profileTextLabel.font = UIFont(name:"Helvetica Neue", size: 14.5)
180
+
181
+ profileTextLabel.textColor = UIColor(red: 212/255, green: 107/255, blue: 0/255, alpha: 1.0)
182
+
183
+ profileTextLabel.textAlignment = .Right
184
+
185
+ profileTextLabel.backgroundColor = UIColor.whiteColor()
186
+
187
+ profileTextLabel.translatesAutoresizingMaskIntoConstraints = false
188
+
189
+ cell.contentView.addSubview(profileTextLabel)
190
+
191
+
192
+
193
+ profileTextLabel.textAlignment = .Left
194
+
195
+ profileTextLabel.tag = 3
196
+
197
+
198
+
199
+ self.view.addConstraints([
200
+
201
+
202
+
203
+ NSLayoutConstraint(
204
+
205
+ item: self.profileTextLabel,
206
+
207
+ attribute: .Left,
208
+
209
+ relatedBy: .Equal,
210
+
211
+ toItem: cell,
212
+
213
+ attribute: .Left,
214
+
215
+ multiplier: 1.0,
216
+
217
+ constant: 140
218
+
219
+ ),
220
+
221
+
222
+
223
+ NSLayoutConstraint(
224
+
225
+ item: self.profileTextLabel,
226
+
227
+ attribute: .Right,
228
+
229
+ relatedBy: .Equal,
230
+
231
+ toItem: cell,
232
+
233
+ attribute: .Right,
234
+
235
+ multiplier: 1.0,
236
+
237
+ constant: -27
238
+
239
+ ),
240
+
241
+
242
+
243
+ NSLayoutConstraint(
244
+
245
+ item: self.profileTextLabel,
246
+
247
+ attribute: .Height,
248
+
249
+ relatedBy: .Equal,
250
+
251
+ toItem: nil,
252
+
253
+ attribute: .Height,
254
+
255
+ multiplier: 1.0,
256
+
257
+ constant: 40
258
+
259
+ ),
260
+
261
+
262
+
263
+ NSLayoutConstraint(
264
+
265
+ item: self.profileTextLabel,
266
+
267
+ attribute: .Top,
268
+
269
+ relatedBy: .Equal,
270
+
271
+ toItem: cell,
272
+
273
+ attribute: .Top,
274
+
275
+ multiplier: 1.0,
276
+
277
+ constant: 0
278
+
279
+ )
280
+
281
+ ])
282
+
283
+
284
+
285
+ default:
286
+
287
+ print("")
288
+
289
+ }
290
+
291
+
292
+
293
+ }
294
+
295
+ return cell
296
+
297
+ }
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+ func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
306
+
307
+
308
+
309
+ switch indexPath.section{
310
+
311
+ case 0:
312
+
313
+
314
+
315
+ if indexPath.row == 0{
316
+
317
+ self.navigationController?.pushViewController(ProfileEditViewController(), animated: true)
318
+
319
+ }
320
+
321
+ default:
322
+
323
+ print("")
324
+
325
+ }
326
+
327
+ }
328
+
329
+
330
+
331
+ //Delegate method
332
+
333
+ func updateProfile(newProfile: String){
334
+
335
+ self.profileTextLabel.text = newProfile
336
+
337
+ self.navigationController?.popViewControllerAnimated(true)
338
+
339
+ }
340
+
341
+
342
+
343
+
344
+
345
+ }
346
+
347
+ ```
348
+
349
+
350
+
351
+ ↓遷移先です。
352
+
353
+ ProfileEditViewControllerのProfileTextFieldで入力された
354
+
355
+ テキストをupdateProfileDelegateプロトコルに
356
+
357
+ セットし、遷移元に渡したいです。
358
+
359
+ ```Swift
360
+
361
+ import UIKit
362
+
363
+
364
+
365
+ //Delegate for updating profile
366
+
367
+ protocol updateProfileDelegate{
368
+
369
+ func updateProfile(newProfile:String)
370
+
371
+ }
372
+
373
+
374
+
375
+ class ProfileEditViewController: UIViewController,UINavigationControllerDelegate{
376
+
377
+
378
+
379
+ var refleshButton: UIButton!
380
+
381
+
382
+
383
+ var profileTextField: UITextField!
384
+
385
+
386
+
387
+ //Delegate for updating profile
388
+
389
+ var delegate: updateProfileDelegate! = nil
390
+
391
+
392
+
393
+ override func viewDidLoad() {
394
+
395
+ super.viewDidLoad()
396
+
397
+
398
+
399
+ self.view.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0)
400
+
401
+
402
+
403
+ self.navigationItem.title = "プロフィール編集"
404
+
405
+
406
+
407
+ refleshButton = UIButton()
408
+
409
+ refleshButton.setTitle("更新", forState: .Normal)
410
+
411
+ refleshButton.addTarget(self, action: "refleshProfile:", forControlEvents: .TouchUpInside)
412
+
413
+ refleshButton.layer.masksToBounds = true
414
+
415
+ refleshButton.layer.cornerRadius = 4.0
416
+
417
+ refleshButton.tintColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0)
418
+
419
+ refleshButton.backgroundColor = UIColor(red: 242/255, green: 147/255, blue: 9/255, alpha: 1.0)
420
+
421
+ refleshButton.translatesAutoresizingMaskIntoConstraints = false
422
+
423
+ self.view.addSubview(refleshButton)
424
+
425
+
426
+
427
+ profileTextField = UITextField()
428
+
429
+ profileTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Top
430
+
431
+ profileTextField.textAlignment = .Left
432
+
433
+ profileTextField.leftView = paddingLeft
434
+
435
+ profileTextField.leftViewMode = UITextFieldViewMode.Always//for left padding
436
+
437
+ profileTextField.translatesAutoresizingMaskIntoConstraints = false
438
+
439
+ self.view.addSubview(profileTextField)
440
+
441
+
442
+
443
+ //Add constraints
444
+
445
+ setupConstraints()
446
+
447
+ }
448
+
449
+
450
+
451
+ //Delegate for updating profile
452
+
453
+ /*******
454
+
455
+ ここでエラーが発生します(エラー内容は添付のスクショをご覧ください)
456
+
457
+ *******/
458
+
459
+ func refleshProfile(sender: AnyObject){
460
+
461
+ self.delegate.updateProfile(profileTextField.text!)
462
+
463
+ }
464
+
465
+
466
+
467
+
468
+
469
+ func setupConstraints(){
470
+
471
+ //文字数の関係で省略します
472
+
473
+ }
474
+
475
+
476
+
477
+
478
+
479
+ }
480
+
481
+ ```
482
+
483
+
484
+
485
+
486
+
487
+ エラー内容
488
+
489
+ ![イメージ説明](1e22ca1d111e4aecfb79dfec01e06e0e.png)

1

加筆

2015/09/26 11:08

投稿

Kesth
Kesth

スコア83

test CHANGED
File without changes
test CHANGED
@@ -91,3 +91,11 @@
91
91
 
92
92
 
93
93
  もしできる場合や他にいい方法があれば教えて頂ければと思います。
94
+
95
+
96
+
97
+ 【参考にしたページ】
98
+
99
+ 自分が書いているソースも下記のような形に近いです
100
+
101
+ [http://tech.eversense.co.jp/153](http://tech.eversense.co.jp/153)