2017-09-22 10 views
0

私のアプリケーションではすべて正常に動作しますが、唯一の問題は、アプリケーションをロードすると、最初のビューが正常に動作しないということです。このダブルクリックの問題を解決するにはどうすればよいですか?

私が間違った答えをクリックすると、アプリは何もしません。そして私は正しい答えを選ぶと2回クリックする必要があります。

どうすればこの問題を解決できますか?

// 
    // EViewController.swift 
    // imageMeaning 
    // 
    // Created by RaduVille on 17/08/17. 
    // Copyright © 2017 RaduVille. All rights reserved. 
    // 

    // 
    // MViewController.swift 
    // imageMeaning 
    // 
    // Created by RaduVille on 17/08/17. 
    // Copyright © 2017 RaduVille. All rights reserved. 
    // 

    import UIKit 

    class EViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     currentQuestion = questions[0] 
     setQuestion() 
     firstOption.isUserInteractionEnabled = true 
     secondOption.isUserInteractionEnabled = true 
     thirdOption.isUserInteractionEnabled = true 
     allowedToGoFurther = 0 

     } 
     override func viewDidAppear(_ animated: Bool) { 

     } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // imageMeaning starts here 
    //layout links to the code starts here 
    //this IBOutlet represints the message shown on top 

    @IBOutlet weak var topMessage: UILabel! 
    // these outlets represints the text shown on the buttons 

    @IBOutlet weak var firstOption: UIButton! 
    @IBOutlet weak var secondOption: UIButton! 
    @IBOutlet weak var thirdOption: UIButton! 
    //represints the images on the right "✅" or "❌" 

    @IBOutlet weak var firstImage: UIImageView! 
    @IBOutlet weak var secondImage: UIImageView! 
    @IBOutlet weak var thirdImage: UIImageView! 

    //this is the big image on the left 

    @IBOutlet weak var leftImage: UIImageView! 
    @IBOutlet weak var nextB: UIButton! 

    @IBAction func nextButton(_ sender: Any) { 

       //this is the next button 
     if allowedToGoFurther == 0 { 
      self.nextB.isEnabled = false 
     } 
     if(currentQuestionPos + 1 < questions.count) { 
      currentQuestionPos += 1 
      currentQuestion = questions[currentQuestionPos] 
      setQuestion() 
      firstImage.image = UIImage(named: "question.png") 
      secondImage.image = UIImage(named: "question.png") 
      thirdImage.image = UIImage(named: "question.png") 
      firstOption.isUserInteractionEnabled = true 
      secondOption.isUserInteractionEnabled = true 
      thirdOption.isUserInteractionEnabled = true 
     } else { 
      loadNextQuestion() 
      firstImage.image = UIImage(named: "question.png") 
      secondImage.image = UIImage(named: "question.png") 
      thirdImage.image = UIImage(named: "question.png") 
     } 
    } 

    struct Question { 
     let image: UIImage 
     let answers: [String] 
     let correctAnswer: Int 
     let corect: String 
    } 

    var questions: [Question] = [ 
     Question(
      image: UIImage(named: "palla")!, 
      answers: ["cerchio", "palla", "aereo"], 
      correctAnswer: 1, 
      corect: "palla"), 
     Question(
      image: UIImage(named: "guanto")!, 
      answers: ["guanto", "maglietta", "calzino"], 
      correctAnswer: 0, 
      corect: "guanto"), 
     Question(
      image: UIImage(named: "casa")!, 
      answers: ["albero", "macchina", "casa"], 
      correctAnswer: 2, 
      corect: "casa"), 
     Question(
      image: UIImage(named: "cerchio")!, 
      answers: ["cerchio", "sole", "palla"], 
      correctAnswer: 0, 
      corect: "cerchio"), 
     Question(
      image: UIImage(named: "lego")!, 
      answers: ["bambola", "lego", "panino"], 
      correctAnswer: 1, 
      corect: "lego"), 
     Question(
      image: UIImage(named: "chiavi")!, 
      answers: ["porta", "pizza", "chiavi"], 
      correctAnswer: 2, 
      corect: "chiavi"), 
     Question(
      image: UIImage(named: "tazza")!, 
      answers: ["tazza", "forchetta", "piatto"], 
      correctAnswer: 0, 
      corect: "tazza"), 
     Question(
      image: UIImage(named: "aereo")!, 
      answers: ["bicicletta", "gelato", "aereo"], 
      correctAnswer: 2, 
      corect: "aereo"), 
     Question(
      image: UIImage(named: "macchina")!, 
      answers: ["televisore", "macchina", "pattini"], 
      correctAnswer: 1, 
      corect: "macchina"), 
     Question(
      image: UIImage(named: "libro")!, 
      answers: ["scatola", "foglio", "libro"], 
      correctAnswer: 2, 
      corect: "libro"), 
     Question(
      image: UIImage(named: "piano")!, 
      answers: ["piano", "chittara", "arpa"], 
      correctAnswer: 0, 
      corect: "piano"), 
     Question(
      image: UIImage(named: "dadi")!, 
      answers: ["dadi", "ghiaccio", "cubo"], 
      correctAnswer: 0, 
      corect: "dadi"), 
     Question(
      image: UIImage(named: "pizza")!, 
      answers: ["hamburger", "panino", "pizza"], 
      correctAnswer: 2, 
      corect: "pizza"), 
     Question(
      image: UIImage(named: "palla")!, 
      answers: ["cerchio", "palla", "aereo"], 
      correctAnswer: 1, 
      corect: "palla") 
    ] 

    var currentQuestion: Question? 
    var currentQuestionPos = 0 
    var noCorrect = 0 
    var Corect = 0 
    var allowedToGoFurther = 0 




    // 
    // 
    // 

    //here starts the code about the first button action 
    @IBAction func firstOption(_ sender: Any) { 
     checkAnswer(idx: 0) 
      } 
    //here starts the code about the second button action 
    @IBAction func secondOption(_ sender: Any) { 
     checkAnswer(idx: 1) 
    } 
    //here starts the code about the third button action 
    @IBAction func thirdOption(_ sender: Any) { 
     checkAnswer(idx: 2) 
    } 

    func checkAnswer(idx: Int) { 
     if(idx == currentQuestion!.correctAnswer) { 
      print("corect") 
      noCorrect += 1 
      let gesture1 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap1(_:))) 
      firstOption.addGestureRecognizer(gesture1) 
      let gesture2 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap2(_:))) 
      secondOption.addGestureRecognizer(gesture2) 
      let gesture3 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap3(_:))) 
      thirdOption.addGestureRecognizer(gesture3) 

     } 
    } 
    @objc func singleTap1(_ recognizer: UIGestureRecognizer) { 
     print("am tastat1") 
     if (firstOption.currentTitle != currentQuestion!.corect) { 
      print("not true, i go further") 
      firstImage.image = UIImage(named: "false.png") 
      firstOption.isUserInteractionEnabled = false 
      secondOption.isUserInteractionEnabled = false 
      thirdOption.isUserInteractionEnabled = false 
      allowedToGoFurther = 1 
      self.nextB.isEnabled = true 

     } else { 
      firstImage.image = UIImage(named: "true.png") 
      firstOption.isUserInteractionEnabled = false 
      secondOption.isUserInteractionEnabled = false 
      thirdOption.isUserInteractionEnabled = false 
      allowedToGoFurther = 1 
      self.nextB.isEnabled = true 
     } 
    } 
    @objc func singleTap2(_ recognizer: UIGestureRecognizer) { 
     print("am tastat2") 
     if (secondOption.currentTitle != currentQuestion!.corect) { 
      print("second button. Not true, I go further") 
      secondImage.image = UIImage(named: "false.png") 
      secondOption.isUserInteractionEnabled = false 
      firstOption.isUserInteractionEnabled = false 
      thirdOption.isUserInteractionEnabled = false 
      allowedToGoFurther = 1 
      self.nextB.isEnabled = true 

     } else { 
      secondImage.image = UIImage(named: "true.png") 
      firstOption.isUserInteractionEnabled = false 
      secondOption.isUserInteractionEnabled = false 
      thirdOption.isUserInteractionEnabled = false 
      allowedToGoFurther = 1 
      self.nextB.isEnabled = true 
     } 
    } 
    @objc func singleTap3(_ recognizer: UIGestureRecognizer) { 
     print("am tastat3") 
     if (thirdOption.currentTitle != currentQuestion!.corect) { 
      print("third button. Not true, I go further") 
      thirdImage.image = UIImage(named: "false.png") 
      thirdOption.isUserInteractionEnabled = false 
      firstOption.isUserInteractionEnabled = false 
      secondOption.isUserInteractionEnabled = false 
      allowedToGoFurther = 1 
      self.nextB.isEnabled = true 

     } else { 
      thirdImage.image = UIImage(named: "true.png") 
      firstOption.isUserInteractionEnabled = false 
      secondOption.isUserInteractionEnabled = false 
      thirdOption.isUserInteractionEnabled = false 
      allowedToGoFurther = 1 
      self.nextB.isEnabled = true 
     } 
    } 
    func loadNextQuestion() { 
     if(currentQuestionPos + 1 < questions.count){ 
      currentQuestionPos += 1 
      currentQuestion = questions[currentQuestionPos] 
      setQuestion() 
      firstOption.isUserInteractionEnabled = true 
      secondOption.isUserInteractionEnabled = true 
      thirdOption.isUserInteractionEnabled = true 
      allowedToGoFurther = 0 
     } else { 
      performSegue(withIdentifier: "easyRestart", sender: nil) 
     } 
    } 
    func setQuestion() { 
     leftImage.image = currentQuestion!.image 
     firstOption.setTitle(currentQuestion!.answers[0], for: .normal) 
     secondOption.setTitle(currentQuestion!.answers[1], for: .normal) 
     thirdOption.setTitle(currentQuestion!.answers[2], for: .normal) 
     self.nextB.isEnabled = false 
    } 
    var easy = 0 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if(segue.identifier == "easyRestart") { 
      let vc = segue.destination as! startOver 
      vc.easy = 1 
      vc.noCorrect = 0 
      vc.total = questions.count 
     } 
    } 

} 

私が間違った答えをクリックすると、100回押しても何もしません。

私は正しい答えを押しても、アプリは何もしません。私がもう一度押すと、それはOKだと私はさらに行くことができると言う。 これは最初の質問でのみ発生します。その後、すべて正常に動作します。

+0

答えを確認しながらオプションボタンにタップジェスチャーを追加する理由は何ですか?あなたがしようとしていることははっきりしていません...あなたのコードを再構成する必要があると思います。 – Rishi

答えて

0

あなたはUIButtonにジェスチャー認識を追加し、UIButtonのデフォルトのジェスチャーが使用してみてください、あなたが追加されたものを取り消すようだ:と

button.addTarget(self, action: #selector(...), for: .touchUpInside) 
あなたのコードで

let gesture1 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap1(_:))) 
    firstOption.addGestureRecognizer(gesture1) 
    let gesture2 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap2(_:))) 
    secondOption.addGestureRecognizer(gesture2) 
    let gesture3 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap3(_:))) 
    thirdOption.addGestureRecognizer(gesture3) 

firstOption.addTarget(self, action: #selector(singleTap1), for: .touchUpInside) 
secondOption.addTarget(self, action: #selector(singleTap2), for: .touchUpInside) 
thirdOption.addTarget(self, action: #selector(singleTap3), for: .touchUpInside) 

パラメータを削除します。

@objc func singleTap1() 
@objc func singleTap2() 
@objc func singleTap3() 

ただし、ボタンを押すたびにターゲットが追加されます。

あなたが達成したいことは本当にわかりません

+0

さて、私は3つの答えで画像クイズアプリを作ろうとしています。左側には画像があり、右側には3つのオプションがあります。私はこれで新しいです..しかし、私はWeb上でスニペットを発見し、stackoverflowの記事と答えを読んで私はそれが今まで働くことができる:)私はコードが混乱している知っている..しかし、私はベストを尽くしているそれをより良くする。 –

+0

ご協力いただきありがとうございます。私はちょうどviewdidload ..にx.addTargetを追加しました。そして今はうまくいきます:)私はそれが正しい方法だと確信していません。 –

関連する問題