質問編集履歴

1

・データの受け渡しにAppDelegateを通さず、ストーリーボードでセグを渡し、prepareForSegueにて受け渡す方法に変更しました。

2016/06/15 14:09

投稿

obataatu
obataatu

スコア14

test CHANGED
File without changes
test CHANGED
@@ -60,10 +60,6 @@
60
60
 
61
61
  var memoList2: [String] = []
62
62
 
63
-
64
-
65
-
66
-
67
63
  var editRow: Int = unselectedRow
68
64
 
69
65
 
@@ -72,6 +68,8 @@
72
68
 
73
69
  super.viewDidLoad()
74
70
 
71
+ print(memo1)
72
+
75
73
 
76
74
 
77
75
  //保存したテキストデータをロード
@@ -112,30 +110,14 @@
112
110
 
113
111
  }
114
112
 
115
-
116
-
117
-
118
-
119
113
  //テキストを受け取る
120
114
 
121
- let app:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
122
-
123
- memo1 = app.title
124
-
125
- memo2 = app.honbun
126
-
127
-
128
-
129
-
130
-
131
115
  //セルにテキストを入れ込む処理
132
116
 
133
117
  applyMemo()
134
118
 
135
119
 
136
120
 
137
-
138
-
139
121
  //編集ボタンを用意
140
122
 
141
123
  navigationItem.rightBarButtonItem = editButtonItem()
@@ -182,21 +164,25 @@
182
164
 
183
165
 
184
166
 
167
+ //指定したセルの番号の配列番号を削除する
168
+
185
169
  memoList.removeAtIndex(indexPath.row)
186
170
 
171
+ memoList2.removeAtIndex(indexPath.row)
172
+
187
173
 
188
174
 
189
175
  tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
190
176
 
191
- //NSUserDefaultsの方のデータも削除
177
+ //NSUserDefaultsの方のデータも削除(ここでは配列の中の一つを削除しており、下のテキストで再度配列全体を保存し直す)
192
178
 
193
179
  let hozon = NSUserDefaults.standardUserDefaults()
194
180
 
195
- hozon.removeObjectForKey("dly")
181
+ hozon.setObject(memoList, forKey: "dly")
196
182
 
197
183
  let hozon3 = NSUserDefaults.standardUserDefaults()
198
184
 
199
- hozon3.removeObjectForKey("dly3")
185
+ hozon3.setObject(memoList2, forKey: "dly3")
200
186
 
201
187
  }
202
188
 
@@ -214,6 +200,10 @@
214
200
 
215
201
  func applyMemo() {
216
202
 
203
+
204
+
205
+
206
+
217
207
  if memo1 == nil {
218
208
 
219
209
  return }
@@ -230,10 +220,14 @@
230
220
 
231
221
  } else {
232
222
 
223
+ print(memo1)
224
+
233
225
  memoList[editRow] = memo1!
234
226
 
235
227
  memoList2[editRow] = memo2!
236
228
 
229
+
230
+
237
231
  }
238
232
 
239
233
 
@@ -250,6 +244,8 @@
250
244
 
251
245
 
252
246
 
247
+ //セルの選択状態を非選択に戻る
248
+
253
249
  editRow = unselectedRow
254
250
 
255
251
  //リロード処理(必須)
@@ -322,31 +318,61 @@
322
318
 
323
319
 
324
320
 
325
- //書き込むページに編集するテキストを受け渡す
326
-
327
- let app:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
328
-
329
- app.title = memo1
321
+ print(memo1)
330
-
331
- app.honbun = memo2
332
-
333
-
334
322
 
335
323
  //書き込みページに画面遷移する
336
324
 
337
- let to_edit = self.storyboard?.instantiateViewControllerWithIdentifier("memo")
325
+
338
-
339
- to_edit?.modalTransitionStyle = .CoverVertical
326
+
340
-
341
- navigationController?.pushViewController(to_edit!, animated: true)
327
+ // SubViewController へ遷移するために Segue を呼び出す
328
+
342
-
329
+ performSegueWithIdentifier("settext1",sender: nil)
343
-
344
-
330
+
331
+
332
+
345
- }
333
+ }
334
+
335
+
336
+
346
-
337
+ @IBAction func memo_tuika(sender: AnyObject) {
338
+
339
+
340
+
347
-
341
+ memo1 = ""
342
+
348
-
343
+ memo2 = ""
344
+
349
-
345
+ performSegueWithIdentifier("settext1",sender: nil)
346
+
347
+
348
+
349
+ }
350
+
351
+
352
+
353
+
354
+
355
+ override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
356
+
357
+ let send2:SecondViewController = segue.destinationViewController as! SecondViewController
358
+
359
+ if segue.identifier == "settext1" {
360
+
361
+ if memo1 == nil {
362
+
363
+ return
364
+
365
+ }
366
+
367
+ send2.title2 = memo1!
368
+
369
+ send2.honbun2 = memo2!
370
+
371
+ print(memo1)
372
+
373
+ }
374
+
375
+ }
350
376
 
351
377
 
352
378
 
@@ -368,6 +394,8 @@
368
394
 
369
395
 
370
396
 
397
+
398
+
371
399
  テキスト入力画面 swift
372
400
 
373
401
 
@@ -388,25 +416,91 @@
388
416
 
389
417
 
390
418
 
419
+ var title2:String?
420
+
421
+ var honbun2:String?
422
+
423
+
424
+
391
425
 
392
426
 
393
427
  override func viewDidLoad() {
394
428
 
395
429
  super.viewDidLoad()
396
430
 
397
-
398
-
399
431
  title_text.delegate = self
400
432
 
401
-
433
+ }
402
-
434
+
435
+
436
+
403
- //編集際にはテキスト受け
437
+ //変更点:前回はセル選択時のテキスト受け取りをviewDidLoad()で行ってしまっており、navigationController?.popViewControllerAnimated(true)で戻ってきた場合、処理が行われていなかったため、毎回の遷移全てで反応すviewWillAppearにて行うよう、変更しました。
404
-
438
+
439
+
440
+
405
- let app:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
441
+ override func viewWillAppear(animated: Bool) {
406
-
442
+
407
- title_text.text = app.title
443
+ title_text.text = self.title2
408
-
444
+
409
- honbun_text.text = app.honbun
445
+ honbun_text.text = self.honbun2
446
+
447
+ }
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+ //移動の際に向こうのプロバティにテキストを引き継ぐ?
456
+
457
+ override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
458
+
459
+
460
+
461
+ if segue.identifier == "sendtext" {
462
+
463
+ let send:memo_list = segue.destinationViewController as! memo_list
464
+
465
+ send.memo1 = title_text.text
466
+
467
+ send.memo2 = honbun_text.text
468
+
469
+
470
+
471
+ //その後、初期化する
472
+
473
+ title_text.text = ""
474
+
475
+ honbun_text.text = ""
476
+
477
+ }
478
+
479
+
480
+
481
+ }
482
+
483
+
484
+
485
+ //キーボードを下げる処理
486
+
487
+ override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
488
+
489
+
490
+
491
+ if(title_text.isFirstResponder()){
492
+
493
+ title_text.resignFirstResponder()
494
+
495
+ }
496
+
497
+ if(honbun_text.isFirstResponder()){
498
+
499
+ honbun_text.resignFirstResponder()
500
+
501
+ }
502
+
503
+
410
504
 
411
505
  }
412
506
 
@@ -414,21 +508,19 @@
414
508
 
415
509
  //保存ボタンアクション
416
510
 
417
- @IBAction func save2(sender: AnyObject) {
511
+ @IBAction func save(sender: AnyObject) {
512
+
418
-
513
+ performSegueWithIdentifier("sendtext",sender: nil)
514
+
515
+
516
+
419
-
517
+ }
420
-
518
+
421
- //書き込まれたキストをAppDelegate受け渡
519
+ //保存(セルの追加)処理をしないで、ーブルビューページ移動る処理
422
-
423
- let app:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
520
+
424
-
425
- app.title = title_text.text
426
-
427
- app.honbun = honbun_text.text
521
+ @IBAction func listbotton(sender: AnyObject) {
428
-
429
-
430
-
431
- //その後、初期化する
522
+
523
+
432
524
 
433
525
  title_text.text = ""
434
526
 
@@ -436,69 +528,33 @@
436
528
 
437
529
 
438
530
 
439
- //その後、テーブルビュー画面に画面遷移する
440
-
441
- let to_list = self.storyboard?.instantiateViewControllerWithIdentifier("memo_list")
442
-
443
- to_list?.modalTransitionStyle = .CoverVertical
444
-
445
- navigationController?.pushViewController(to_list!, animated: true)
531
+ performSegueWithIdentifier("sendtext",sender: nil)
446
-
532
+
533
+
534
+
447
- }
535
+ }
448
-
449
-
450
-
536
+
537
+
538
+
451
- //キーードを下げる処理
539
+ //仮で作った戻るタン
452
-
453
- override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
540
+
454
-
455
-
456
-
457
- if(title_text.isFirstResponder()){
458
-
459
- title_text.resignFirstResponder()
541
+ //prepareForSegueが反応してくれない
542
+
460
-
543
+ @IBAction func back(sender: UIStoryboardSegue) {
544
+
545
+
546
+
547
+ let x = self.storyboard?.instantiateViewControllerWithIdentifier("memo_list")
548
+
549
+ x?.modalTransitionStyle = .CoverVertical
550
+
551
+
552
+
553
+ navigationController?.popViewControllerAnimated(true)
554
+
555
+
556
+
461
- }
557
+ }
462
-
463
- if(honbun_text.isFirstResponder()){
464
-
465
- honbun_text.resignFirstResponder()
466
-
467
- }
468
-
469
-
470
-
471
- }
472
-
473
-
474
-
475
- //保存処理をしないで、テーブルビューページに移動する処理
476
-
477
- @IBAction func listbotton(sender: AnyObject) {
478
-
479
-
480
-
481
- let app:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
482
-
483
- app.title = ""
484
-
485
- app.honbun = ""
486
-
487
-
488
-
489
- let to_list = self.storyboard?.instantiateViewControllerWithIdentifier("memo_list")
490
-
491
- to_list?.modalTransitionStyle = .CoverVertical
492
-
493
- navigationController?.pushViewController(to_list!, animated: true)
494
-
495
-
496
-
497
- }
498
-
499
-
500
-
501
-
502
558
 
503
559
 
504
560
 
@@ -518,6 +574,10 @@
518
574
 
519
575
 
520
576
 
577
+
578
+
579
+
580
+
521
581
  AppDelegate swift
522
582
 
523
583