回答編集履歴

3

Show

2016/09/08 06:47

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -89,3 +89,31 @@
89
89
  ```
90
90
 
91
91
 
92
+
93
+ ###### 【おまけ】Showの場合
94
+
95
+
96
+
97
+ ```swift
98
+
99
+ //Navigation Controllerを取得
100
+
101
+ let nav = self.navigationController!
102
+
103
+
104
+
105
+ //呼び出し元のView Controllerを遷移履歴から取得しパラメータを渡す
106
+
107
+ let InfoVc = nav.viewControllers[nav.viewControllers.count-2] as! FirstViewController
108
+
109
+ InfoVc.paramCellNum = arrayHistoryEventKey[indexPath.row]
110
+
111
+
112
+
113
+ //閉じる
114
+
115
+ self.navigationController?.popViewControllerAnimated(true)
116
+
117
+ ```
118
+
119
+

2

回答修正および清書。

2016/09/08 06:47

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -10,7 +10,17 @@
10
10
 
11
11
 
12
12
 
13
- HistoryViewControllerが**Present Modally**で呼ばれていると仮定して、遷移元は`presentingViewController`で取得できますので、
13
+ HistoryViewControllerが**Present Modally**で呼ばれていると仮定します
14
+
15
+
16
+
17
+ ## Navigation Controllerを使用していない場合
18
+
19
+
20
+
21
+ こちらは簡単です。
22
+
23
+ 呼び出し元は`presentingViewController`で取得できますので、
14
24
 
15
25
 
16
26
 
@@ -20,15 +30,9 @@
20
30
 
21
31
 
22
32
 
23
- //遷移パラメータを渡す
33
+ //呼び出しのView Controllerを取得しパラメータを渡す
24
34
 
25
- //NavigationControllerを使っていない場合
26
-
27
- //let InfoVc = self.presentingViewController as! FirstViewController
35
+ let InfoVc = self.presentingViewController as! FirstViewController
28
-
29
- //NavigationControllerを使っている場合
30
-
31
- let InfoVc = self.navigationController.viewControllers[self.navigationController.viewControllers.count-2] as! FirstViewController
32
36
 
33
37
  InfoVc.paramCellNum = arrayHistoryEventKey[indexPath.row]
34
38
 
@@ -44,4 +48,44 @@
44
48
 
45
49
 
46
50
 
51
+ ## Navigation Controllerを使用している場合
52
+
53
+
54
+
55
+ こちらはちょっと面倒です。
56
+
57
+ `presentingViewController`にはNavigation Controllerが入っていますので、まずそれを取得します。
58
+
59
+ Navigation Controllerの`viewControllers`には、今までの遷移履歴が入っていますので、そこから呼び出し元(履歴の最後)のView Controllerを取得します。
60
+
61
+
62
+
47
- で、どうでしょうか?
63
+ ```swift
64
+
65
+ override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
66
+
67
+
68
+
69
+ //Navigation Controllerを取得
70
+
71
+ let nav = self.presentingViewController as! UINavigationController
72
+
73
+
74
+
75
+ //呼び出し元のView Controllerを遷移履歴から取得しパラメータを渡す
76
+
77
+ let InfoVc = nav.viewControllers[nav.viewControllers.count-1] as! FirstViewController
78
+
79
+ InfoVc.paramCellNum = arrayHistoryEventKey[indexPath.row]
80
+
81
+
82
+
83
+ //閉じる
84
+
85
+ self.dismissViewControllerAnimated(true, completion: nil)
86
+
87
+ }
88
+
89
+ ```
90
+
91
+

1

NavigationController使用。

2016/09/08 00:23

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -22,7 +22,13 @@
22
22
 
23
23
  //遷移元にパラメータを渡す
24
24
 
25
+ //NavigationControllerを使っていない場合
26
+
25
- let InfoVc = self.presentingViewController as! FirstViewController
27
+ //let InfoVc = self.presentingViewController as! FirstViewController
28
+
29
+ //NavigationControllerを使っている場合
30
+
31
+ let InfoVc = self.navigationController.viewControllers[self.navigationController.viewControllers.count-2] as! FirstViewController
26
32
 
27
33
  InfoVc.paramCellNum = arrayHistoryEventKey[indexPath.row]
28
34