回答編集履歴

2

コードの追記をしたことが目立つようにしました。

2022/06/09 14:14

投稿

退会済みユーザー
test CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ---
11
11
 
12
- 追記です。
12
+ ### ********** 追記です。 **********
13
13
 
14
14
  > ①の「+」ボタンを押すことで、③の画面に遷移し、「完了」ボタンを押すと④に遷移したいのですが、
15
15
 

1

完了ボタンタップ時のコメントに関する回答を追記しました。

2022/06/09 06:35

投稿

退会済みユーザー
test CHANGED
@@ -7,3 +7,86 @@
7
7
 
8
8
  https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621886-popviewcontroller
9
9
 
10
+ ---
11
+
12
+ 追記です。
13
+
14
+ > ①の「+」ボタンを押すことで、③の画面に遷移し、「完了」ボタンを押すと④に遷移したいのですが、
15
+
16
+ 見た目の動きがちょっとどうかな?と思いますが、次のような感じはどうでしょうか。
17
+ (①が一瞬表示される感じになってしまうようでした・・)
18
+
19
+ ```swift
20
+ import UIKit
21
+
22
+ class AppState {
23
+ static var isEdited = false // ③から完了をタップして戻ってきたことを表すフラグです。
24
+ }
25
+ // ①のビューコントローラー
26
+ class ViewController1: UITableViewController {
27
+ override func viewDidLoad() {
28
+ super.viewDidLoad()
29
+ let item = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(action))
30
+ navigationItem.rightBarButtonItem = item
31
+ }
32
+ @objc func action() {
33
+ let vc = storyboard?.instantiateViewController(withIdentifier: "view3") as! ViewController3
34
+ navigationController?.pushViewController(vc, animated: true)
35
+ }
36
+ override func viewWillAppear(_ animated: Bool) {
37
+ // TODO: ①に戻ってきたらUITableViewを再描画します。
38
+ if AppState.isEdited {
39
+ // ③から完了をタップして戻ってきた場合は②に遷移します。
40
+ AppState.isEdited = false
41
+ let vc = storyboard?.instantiateViewController(withIdentifier: "view2") as! ViewController2
42
+ navigationController?.pushViewController(vc, animated: true)
43
+ }
44
+ }
45
+ override func numberOfSections(in tableView: UITableView) -> Int {
46
+ return 1
47
+ }
48
+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
49
+ return 1
50
+ }
51
+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
52
+ let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
53
+ return cell
54
+ }
55
+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
56
+ tableView.deselectRow(at: indexPath, animated: true)
57
+ let vc = storyboard?.instantiateViewController(withIdentifier: "view2") as! ViewController2
58
+ navigationController?.pushViewController(vc, animated: true)
59
+ }
60
+ }
61
+
62
+ // ②のビューコントローラー
63
+ class ViewController2: UIViewController {
64
+ override func viewDidLoad() {
65
+ super.viewDidLoad()
66
+ let item = UIBarButtonItem(title: "編集", style: .plain, target: self, action: #selector(action))
67
+ navigationItem.rightBarButtonItem = item
68
+ }
69
+ @objc func action() {
70
+ let vc = storyboard?.instantiateViewController(withIdentifier: "view3") as! ViewController3
71
+ navigationController?.pushViewController(vc, animated: true)
72
+ }
73
+ }
74
+
75
+ // ③のビューコントローラー
76
+ class ViewController3: UIViewController {
77
+ override func viewDidLoad() {
78
+ super.viewDidLoad()
79
+ let item = UIBarButtonItem(title: "完了", style: .plain, target: self, action: #selector(action))
80
+ navigationItem.rightBarButtonItem = item
81
+ }
82
+ @objc func action() {
83
+ if let n = navigationController,
84
+ n.viewControllers[n.viewControllers.count - 2] is ViewController1 {
85
+ // ①から遷移してきた場合は完了をタップしたことを表すフラグを設定します。
86
+ AppState.isEdited = true
87
+ }
88
+ navigationController?.popViewController(animated: true)
89
+ }
90
+ }
91
+ ```
92
+