回答編集履歴

2

Show

2018/08/21 07:33

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -13,6 +13,10 @@
13
13
 
14
14
 
15
15
  # 追記
16
+
17
+
18
+
19
+ ボタンのAction Segueの`Show`で遷移していると仮定して。
16
20
 
17
21
 
18
22
 

1

追記

2018/08/21 07:33

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -9,3 +9,95 @@
9
9
  これは新規にインスタンスを生成していますので、当然`Player_Hand`も`CPU_Hand`もカラです。
10
10
 
11
11
  新規に生成するのでははなく、生成済のインスタンスを取得して下さい。(もしくは`Player_Hand`と`CPU_Hand`を受け取るか)
12
+
13
+
14
+
15
+ # 追記
16
+
17
+
18
+
19
+ ```swift
20
+
21
+ class ResultViewController: UIViewController {
22
+
23
+ :
24
+
25
+ //ChoiceHandViewControllerのインスタンスを作成
26
+
27
+ //let choice_instance = ChoiceHandViewController() //※これは使わない
28
+
29
+ :
30
+
31
+ override func viewDidLoad() {
32
+
33
+ super.viewDidLoad()
34
+
35
+
36
+
37
+ //ここでは取得しない
38
+
39
+ }
40
+
41
+
42
+
43
+ override func viewWillAppear(_ animated: Bool) {
44
+
45
+ super.viewWillAppear(animated)
46
+
47
+
48
+
49
+ //viewDidLoad()ではpresentingViewControllerを取得できないので、ここで取得する
50
+
51
+ displayresult()
52
+
53
+ result()
54
+
55
+ }
56
+
57
+
58
+
59
+ //両者の手を表示
60
+
61
+ func displayresult() {
62
+
63
+
64
+
65
+ //ChoiceHandViewControllerのインスタンスから、それぞれの手を取得する
66
+
67
+ if let choice_instance = self.presentingViewController as? ChoiceHandViewController {
68
+
69
+ //プレイヤーの手を表示
70
+
71
+ PlayerLabel.text = choice_instance.Player_Hand
72
+
73
+ //CPUの手を表示
74
+
75
+ CPULabel.text = choice_instance.CPU_Hand
76
+
77
+ } else {
78
+
79
+ //念のため取得失敗の処理
80
+
81
+ PlayerLabel.text = "?"
82
+
83
+ CPULabel.text = "?"
84
+
85
+ }
86
+
87
+ }
88
+
89
+ :
90
+
91
+ }
92
+
93
+ ```
94
+
95
+
96
+
97
+ これもあまりいい方法とは言えません。
98
+
99
+
100
+
101
+ 最初にもチラと書きましたが、ResultViewController側に`Player_Hand`と`CPU_Hand`を持たせて、ChoiceHandViewControllerからResultViewControllerに**結果を渡す**方がいいです。
102
+
103
+ 遷移時の値の受け渡しには[prepare(for:sender:)](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepare)を使います。