###実現したいこと・やりたいこと
プログラミング初心者です。クイズアプリを作ろうと思っています。そこでSegueを使って送られた値を変えてそれによって問題を変えたいです。そのコードを書いてみたのですが、うまく動作しません。そこで、質問させていただきました。
###問題になっていること
実行すると、どちらも同じif文の最初の結果になる。
###コード
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 let a = "problem1" 6 let b = "problem2" 7 8 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 } 14 15 @IBAction func returnToTop(segue:UIStoryboardSegue){} 16 17 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 18 19 if segue.identifier == "sayFirst"{ 20 let secondVc = segue.destination as! SecondViewController 21 secondVc.a = self.a 22 }else if segue.identifier == "saySecond"{ 23 let secondVc = segue.destination as! SecondViewController 24 secondVc.b = self.b 25 26 } 27 28 } 29 30} 31 32
Swift
1 2import UIKit 3 4class SecondViewController: UIViewController { 5 6 var a = "" 7 var b = "" 8 9 @IBOutlet weak var Button1: UIButton! 10 @IBOutlet weak var Button2: UIButton! 11 @IBOutlet weak var Button3: UIButton! 12 @IBOutlet weak var Button4: UIButton! 13 @IBOutlet weak var aLabel: UILabel! 14 @IBOutlet weak var numberLabel: UILabel! 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 RandomQuestion() 19 } 20 21 22 23 func RandomQuestion() { 24 if case var a = "" { 25 numberLabel.text = "1" 26 aLabel.text = "日本から見て西にあり、世界一人口が多い国は?" 27 Button1.setTitle("韓国", for: UIControl.State.normal) 28 Button2.setTitle("ロシア", for: UIControl.State.normal) 29 Button3.setTitle("インド", for: UIControl.State.normal) 30 Button4.setTitle("アメリカ", for: UIControl.State.normal) 31 32 }else if case var b = ""{ 33 numberLabel.text = "2" 34 aLabel.text = "日本から見て、地球の裏側の国はどこ?" 35 Button1.setTitle("アメリカ", for: UIControl.State.normal) 36 Button2.setTitle("ロシア", for: UIControl.State.normal) 37 Button3.setTitle("イギリス", for: UIControl.State.normal) 38 Button4.setTitle("ブラジル", for: UIControl.State.normal) 39 }else{} 40 41 // Do any additional setup after loading the view. 42 } 43
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/27 09:46