##前提・実現したいこと
swift初心者です。くだらない質問です
本当に初歩的な質問で申し訳ありません。
VC間で「見た・見てない」の判定で画像を写して判断してもらい、
何を選んだかををtagで判断しています。
選んだものを最終的に結果画面表示時に点数化しわかるようにしたいです。
今困っているのは、変数をVC間に記入していると「Use of unresolved identifier...」と
実行できずこのエラーが出てしまいます。
これを修正し、画面遷移とこのtagがしっかり判定されているのを確認したいのですが、
修正方法がわかりません。教えていただきたいです。
##エラー
VCのUIButtonによる画面遷移で次のVCに遷移が出来ないでいます。
Use of unresolved identifier 'ViewController_r4'
##対象コード
ViewController_r3、ViewController_r4のようにこの形のものが残り3つほどが画面遷移でつながっています。
この全てのVC間で出来ずにいます。下記のコードは一部です。
swift
1var r3answer:Int = 0 2 3class ViewController_r3:UIViewController{ 4 5 6 7 override func loadView(){ 8 let view = UIView() 9 view.frame = CGRect(x:0, y:0, width:375, height:669) 10 view.backgroundColor = .white 11 self.view = view 12 13 let image1:UIImage = UIImage(named:"画像")! 14 let imageView1 = UIImageView(image:image1) 15 16 let screenWidth:CGFloat = view.frame.size.width 17 let screenHeight:CGFloat = view.frame.size.height 18 19 imageView1.center = CGPoint(x:screenWidth/2, y:screenHeight/2) 20 imageView1.frame = CGRect(x:85, y:150, width:200, height:200) 21 view.addSubview(imageView1) 22 23 let btn_r31 = UIButton() 24 btn_r31.frame = CGRect(x:110, y:450, width:150, height:50) 25 btn_r31.setTitle("見た", for:.normal) 26 btn_r31.setTitleColor(UIColor.white, for:.highlighted) 27 btn_r31.backgroundColor = .blue 28 btn_r31.layer.cornerRadius = 20.0 29 btn_r31.addTarget(self, action:#selector(onClick(_:)), for:.touchUpInside) 30 btn_r31.tag = 1 31 view.addSubview(btn_r31) 32 33 let btn_r32 = UIButton() 34 btn_r32.frame = CGRect(x:110, y:510, width:150, height:50) 35 btn_r32.setTitle("見ていない", for:.normal) 36 btn_r32.setTitleColor(UIColor.white, for:.highlighted) 37 btn_r32.backgroundColor = .blue 38 btn_r32.layer.cornerRadius = 20.0 39 btn_r32.addTarget(self, action:#selector(onClick(_:)), for:.touchUpInside) 40 btn_r32.tag = 2 41 view.addSubview(btn_r32) 42 43 44 } 45 @objc func onClick(_ sender:UIButton){ 46 print(sender.tag); 47 48 let nextvc = ViewController_r4() //ここにエラーが出ます 49 if sender.tag == 2 { 50 r3answer = 0 51 } 52 self.present(nextvc, animated:true, completion: nil) 53 54 } 55 @objc func tappB(_ sender:UIButton){ 56 57 let nextvc = ViewController_r3() 58 59 self.present(nextvc, animated: true, completion: nil) 60 } 61 62} 63 64 65var r4answer:Int = 0 66class ViewController_r4:UIViewController { 67 68 override func loadView() { 69 70 let view = UIView() 71 view.frame = CGRect(x:0, y:0, width:375, height:669) 72 view.backgroundColor = .white 73 self.view = view 74 75 let image2:UIImage = UIImage(named:"画像")! 76 let imageView2 = UIImageView(image:image2) 77 78 let screenWidth:CGFloat = view.frame.size.width 79 let screenHeight:CGFloat = view.frame.size.height 80 81 imageView2.center = CGPoint(x:screenWidth/2, y:screenHeight/2) 82 imageView2.frame = CGRect(x:70, y:150, width:220, height:230) 83 view.addSubview(imageView2) 84 85 let btn_r41 = UIButton() 86 btn_r41.frame = CGRect(x:110, y:450, width:150, height:50) 87 btn_r41.setTitle("見た", for:.normal) 88 btn_r41.setTitleColor(UIColor.white, for:.highlighted) 89 btn_r41.backgroundColor = .blue 90 btn_r41.layer.cornerRadius = 20.0 91 btn_r41.addTarget(self, action:#selector(onClick(_:)), for:.touchUpInside) 92 btn_r41.tag = 1 93 view.addSubview(btn_r41) 94 95 let btn_r42 = UIButton() 96 btn_r42.frame = CGRect(x:110, y:510, width:150, height:50) 97 btn_r42.setTitle("見ていない", for:.normal) 98 btn_r42.setTitleColor(UIColor.white, for:.highlighted) 99 btn_r42.backgroundColor = .blue 100 btn_r42.layer.cornerRadius = 20.0 101 btn_r42.addTarget(self, action:#selector(onClick(_:)), for:.touchUpInside) 102 btn_r42.tag = 2 103 view.addSubview(btn_r42) 104 105 } 106 @objc func onClick(_ sender:UIButton){ 107 print(sender.tag); 108 109 let nextvc = ViewController_r5() 110 if sender.tag == 2 { 111 r4answer = 0 112 } 113 self.present(nextvc, animated: true, completion: nil) 114 } 115 116}