質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

2回答

2361閲覧

【Swift,Xcode】Buttonで格納したはずの文字列がUILabelに表示されない

nekokichi

総合スコア54

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2018/08/21 03:45

前提・実現したいこと

「ChoiceViewController」
イメージ説明

「ResultViewController」
!イメージ説明

1つ目の画面にある、上右下左のいずれかを押したら、
2つ目の画面にある、「あなた」の下部に選択した矢印が表示され、
「ホイッ!!」の下部にはランダムに選択された矢印が表示されるようにしたいです。

上右下左のいずれかをタップすると、ResultViewControllerに切り替わる仕組みになってます。

発生している問題・エラーメッセージ

ソースコードでは、合っているはずなんですが、ResultViewControllerの「ホイッ!!」と「あなた」のそれぞれの下部に配置したUILabelがなぜか表示されません。 原因がわからずつまづいています。 どうかご回答よろしくお願いします。

該当のソースコード

**「ChoiceViewController」 ** import UIKit class ChoiceHandViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. random_cpuHand() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //上右下左の手を用意 let Hand = ["⬆︎","➡︎","⬇︎","⬅︎"] //プレイヤーの手 var Player_Hand = "" //CPUの手 var CPU_Hand = "" //CPUの手をランダムに選択 //配列の番号にUInt32は使えない!? func random_cpuHand() { let random = Int(arc4random_uniform(4)) CPU_Hand += Hand[random] } //上を選択 @IBAction func UpButton(_ sender: Any) { Player_Hand += Hand[0] } //右を選択 @IBAction func RightButton(_ sender: Any) { Player_Hand += Hand[1] } //下を選択 @IBAction func DownButton(_ sender: Any) { Player_Hand += Hand[2] } //左を選択 @IBAction func LeftButton(_ sender: Any) { Player_Hand += Hand[3] } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } **「ResultViewController」** import UIKit class ResultViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. displayresult() result() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBOutlet weak var CPULabel: UILabel! @IBOutlet weak var PlayerLabel: UILabel! //勝敗の判定を表す変数 var winorlose = "" //スコアを保持する変数 var score = 0 //ChoiceHandViewControllerのインスタンスを作成 let choice_instance = ChoiceHandViewController() //勝敗を判定する処理 func result() { if choice_instance.Player_Hand == choice_instance.CPU_Hand { winorlose = "lose" } else { winorlose = "win" score += 1 } } //両者の手を表示 func displayresult() { //プレイヤーの手を表示 PlayerLabel.text = choice_instance.Player_Hand //CPUの手を表示 CPULabel.text = choice_instance.CPU_Hand } //プレイヤーが勝ちなら前画面に戻る、負けならゲーム終了 @IBAction func changeViewButton(_ sender: Any) { if winorlose == "win" { performSegue(withIdentifier: "Win", sender: nil) } else if winorlose == "lose" { performSegue(withIdentifier: "Lose", sender: nil) } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }

試したこと

・ChoiceViewControllerの@IBActionに、print(Player_Hand)を追加したら、確かにPlayer_Handには選択した矢印が表示されていました。

・ func displayresult() {
//プレイヤーの手を表示
PlayerLabel.text = choice_instance.Player_Hand
//CPUの手を表示
CPULabel.text = choice_instance.CPU_Hand
print(choice_instance.Player_Hand)
print(choice_instance.CPU_Hand)
}
のように記述しましたが、コンソール画面には何も表示されませんでした。

補足情報(FW/ツールのバージョンなど)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/08/21 04:01 編集

(deleted)
fuzzball

2018/08/21 06:51

ChoiceHandViewControllerからResultViewControllerには、どうやって遷移していますか?
guest

回答2

0

まずChoiceHandViewControllerの変更点
ChoiceHandViewControllerのクラス外でインスタンスを作成しておく

swift

1//ChoiceHandViewController 2 3var result = ChoiceHandViewController() 4 5class ChoiceHandViewController: UIViewController { 6//省略

各変数の前にresult(作成したインスタンス)を加える

swift

1//ChoiceHandViewController 2 3func random_cpuHand() { 4 let random = Int(arc4random_uniform(4)) 5 result.CPU_Hand += Hand[random] 6 } 7 8 //上を選択 9 @IBAction func UpButton(_ sender: Any) { 10 result.Player_Hand += Hand[0] 11 } 12 13 //右を選択 14 @IBAction func RightButton(_ sender: Any) { 15 result.Player_Hand += Hand[1] 16 } 17 18 //下を選択 19 @IBAction func DownButton(_ sender: Any) { 20 result.Player_Hand += Hand[2] 21 } 22 23 //左を選択 24 @IBAction func LeftButton(_ sender: Any) { 25 result.Player_Hand += Hand[3] 26 }

次はResultViewControllerの変更点

swift

1class ResultViewController: UIViewController { 2 3 let choice_instance = result 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 print(choice_instance.Player_Hand) 9 print(choice_instance.CPU_Hand) 10 11 }

多分あまり綺麗なやり方ではないですが、とりあえず動いて次に進めたいのであればこんな感じでいけると思います。
動かなかったらすいません!
エラーに負けず頑張ってください!!

投稿2018/08/21 06:33

bokuranokyo

総合スコア16

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nekokichi

2018/08/21 06:45

ありがとうございます!! 無事、動作してくれました!! 申し訳ありませんが、お答えしていただけますか? 1)なぜ、let choice_instance = result、によってChoiceViewControllerのインスタンスをResultViewContollerに渡せたのか? 2)なぜ、ChoiceViewControllerにおいて、var result = ChoiceHandViewController()、の必要があったのか? 3)なぜ、var result = ChoiceHandViewController()、をclassよりも上の方に記述したのか? できれば疑問を解決してから次に行きたいので、1つでも構いませんので、上記の質問にお答えしていただけますか? よろしくお願いします!!
bokuranokyo

2018/08/21 07:00

動作してよかったです!! それがお恥ずかしいことに自分も体系的に学んできたわけではないので詳しくは答えられません、、 3)に関しては、そもそもインスタンスはclassの外に書くものなので上に書いておきました。
nekokichi

2018/08/21 07:01

全然知りませんでした..。 本当に助かりました!!
guest

0

ベストアンサー

swift

1let choice_instance = ChoiceHandViewController()

これは新規にインスタンスを生成していますので、当然Player_HandCPU_Handもカラです。
新規に生成するのでははなく、生成済のインスタンスを取得して下さい。(もしくはPlayer_HandCPU_Handを受け取るか)

追記

ボタンのAction SegueのShowで遷移していると仮定して。

swift

1class ResultViewController: UIViewController { 2 : 3 //ChoiceHandViewControllerのインスタンスを作成 4 //let choice_instance = ChoiceHandViewController() //※これは使わない 5 : 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 9 //ここでは取得しない 10 } 11 12 override func viewWillAppear(_ animated: Bool) { 13 super.viewWillAppear(animated) 14 15 //viewDidLoad()ではpresentingViewControllerを取得できないので、ここで取得する 16 displayresult() 17 result() 18 } 19 20 //両者の手を表示 21 func displayresult() { 22 23 //ChoiceHandViewControllerのインスタンスから、それぞれの手を取得する 24 if let choice_instance = self.presentingViewController as? ChoiceHandViewController { 25 //プレイヤーの手を表示 26 PlayerLabel.text = choice_instance.Player_Hand 27 //CPUの手を表示 28 CPULabel.text = choice_instance.CPU_Hand 29 } else { 30 //念のため取得失敗の処理 31 PlayerLabel.text = "?" 32 CPULabel.text = "?" 33 } 34 } 35 : 36}

これもあまりいい方法とは言えません。

最初にもチラと書きましたが、ResultViewController側にPlayer_HandCPU_Handを持たせて、ChoiceHandViewControllerからResultViewControllerに結果を渡す方がいいです。
遷移時の値の受け渡しにはprepare(for:sender:)を使います。

投稿2018/08/21 04:06

編集2018/08/21 07:33
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nekokichi

2018/08/21 04:12

ご回答ありがとうございます。 生成済みのインスタンスの定型文を教えていただけますか? お手数をかけて申し訳ありません。
nekokichi

2018/08/21 07:01

無事解決しました。 お手数をかけて申し訳ありませんでした。 2度とこのようなことがないようにします。
fuzzball

2018/08/21 07:05

bokuranokyoさんの回答で解決しようとしているのでしたら、やめておいた方がいいです。 「質問への追記・修正」の質問への返答をお願いします。
nekokichi

2018/08/21 07:57

遷移時に値を渡せるメソッドを最初から使えてれば、もっと楽に記述できたかもしれませんね。 もっと詳しくググればよかったですね。 prepareメソッドを教えていただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問