回答編集履歴
2
Show
answer
CHANGED
@@ -7,6 +7,8 @@
|
|
7
7
|
|
8
8
|
# 追記
|
9
9
|
|
10
|
+
ボタンのAction Segueの`Show`で遷移していると仮定して。
|
11
|
+
|
10
12
|
```swift
|
11
13
|
class ResultViewController: UIViewController {
|
12
14
|
:
|
1
追記
answer
CHANGED
@@ -3,4 +3,50 @@
|
|
3
3
|
```
|
4
4
|
|
5
5
|
これは新規にインスタンスを生成していますので、当然`Player_Hand`も`CPU_Hand`もカラです。
|
6
|
-
新規に生成するのでははなく、生成済のインスタンスを取得して下さい。(もしくは`Player_Hand`と`CPU_Hand`を受け取るか)
|
6
|
+
新規に生成するのでははなく、生成済のインスタンスを取得して下さい。(もしくは`Player_Hand`と`CPU_Hand`を受け取るか)
|
7
|
+
|
8
|
+
# 追記
|
9
|
+
|
10
|
+
```swift
|
11
|
+
class ResultViewController: UIViewController {
|
12
|
+
:
|
13
|
+
//ChoiceHandViewControllerのインスタンスを作成
|
14
|
+
//let choice_instance = ChoiceHandViewController() //※これは使わない
|
15
|
+
:
|
16
|
+
override func viewDidLoad() {
|
17
|
+
super.viewDidLoad()
|
18
|
+
|
19
|
+
//ここでは取得しない
|
20
|
+
}
|
21
|
+
|
22
|
+
override func viewWillAppear(_ animated: Bool) {
|
23
|
+
super.viewWillAppear(animated)
|
24
|
+
|
25
|
+
//viewDidLoad()ではpresentingViewControllerを取得できないので、ここで取得する
|
26
|
+
displayresult()
|
27
|
+
result()
|
28
|
+
}
|
29
|
+
|
30
|
+
//両者の手を表示
|
31
|
+
func displayresult() {
|
32
|
+
|
33
|
+
//ChoiceHandViewControllerのインスタンスから、それぞれの手を取得する
|
34
|
+
if let choice_instance = self.presentingViewController as? ChoiceHandViewController {
|
35
|
+
//プレイヤーの手を表示
|
36
|
+
PlayerLabel.text = choice_instance.Player_Hand
|
37
|
+
//CPUの手を表示
|
38
|
+
CPULabel.text = choice_instance.CPU_Hand
|
39
|
+
} else {
|
40
|
+
//念のため取得失敗の処理
|
41
|
+
PlayerLabel.text = "?"
|
42
|
+
CPULabel.text = "?"
|
43
|
+
}
|
44
|
+
}
|
45
|
+
:
|
46
|
+
}
|
47
|
+
```
|
48
|
+
|
49
|
+
これもあまりいい方法とは言えません。
|
50
|
+
|
51
|
+
最初にもチラと書きましたが、ResultViewController側に`Player_Hand`と`CPU_Hand`を持たせて、ChoiceHandViewControllerからResultViewControllerに**結果を渡す**方がいいです。
|
52
|
+
遷移時の値の受け渡しには[prepare(for:sender:)](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepare)を使います。
|