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

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

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

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

Q&A

1回答

407閲覧

クイズアプリの問題の止め方

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

1クリップ

投稿2018/02/05 02:29

前提・実現したいこと

初心者です。
Xcodeで5択のクイズアプリを作成しています。
クイズ問題と5択の選択肢が出題され、
正解の場合は正解音と共に次の問題へ移り、
不正解の場合は不正解音と共に正解が表示された後に、次の問題に移ります。
現在は次の問題がなくなったら出題を終了し正答率の画面に遷移するようになっています。
これを50問終了した時点で遷移するようにするためにはどのようなコードを書けばいいのでしょうか。
もしくはヒントとなるサイトだけでも教えていただければ幸いです。

該当のソースコード

Swift

1import UIKit 2 3class QuestionJViewController: UIViewController { 4 5 var questionJData: QuestionJData! 6 7 @IBOutlet weak var questionNoLabel: UILabel! 8 @IBOutlet weak var questionYearLabel: UILabel! 9 @IBOutlet weak var questionTextView: UITextView! 10 @IBOutlet weak var questionFieldLabel: UILabel! 11 12 @IBOutlet weak var answerJ1Button: UIButton! 13 @IBOutlet weak var answerJ2Button: UIButton! 14 @IBOutlet weak var answerJ3Button: UIButton! 15 @IBOutlet weak var answerJ4Button: UIButton! 16 @IBOutlet weak var answerJ5Button: UIButton! 17 18 @IBOutlet weak var correctImageView: UIImageView! 19 @IBOutlet weak var incorrectImageView: UIImageView! 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 24 // Do any additional setup after loading the view. 25 26 //初期データ設定処理。前画面で設定済みのQuestionDataから値を取り出す 27 questionNoLabel.text = "Q.(questionJData.questionNo)" 28 questionYearLabel.text = "(questionJData.questionYear)" 29 questionFieldLabel.text = questionJData.questionField 30 questionTextView.text = questionJData.question 31 answerJ1Button.setTitle(questionJData.answer1, for: UIControlState.normal) 32 answerJ2Button.setTitle(questionJData.answer2, for: UIControlState.normal) 33 answerJ3Button.setTitle(questionJData.answer3, for: UIControlState.normal) 34 answerJ4Button.setTitle(questionJData.answer4, for: UIControlState.normal) 35 answerJ5Button.setTitle(questionJData.answer5, for: UIControlState.normal) 36 } 37 38 override func didReceiveMemoryWarning() { 39 super.didReceiveMemoryWarning() 40 // Dispose of any resources that can be recreated. 41 } 42 43 44 /* 45 // MARK: - Navigation 46 47 // In a storyboard-based application, you will often want to do a little preparation before navigation 48 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 49 // Get the new view controller using segue.destinationViewController. 50 // Pass the selected object to the new view controller. 51 } 52 */ 53 54 //選択肢1をタップ 55 @IBAction func tapAnswerJ1Button(_ sender: Any) { 56 questionJData.userChoiceAnswerNumber = 1 //選択した答えの番号を保存する 57 goNextQuestionWithAnimation() //次の問題に進む 58 } 59 60 //選択肢2をタップ 61 @IBAction func tapAnswerJ2Button(_ sender: Any) { 62 questionJData.userChoiceAnswerNumber = 2 //選択した答えの番号を保存する 63 goNextQuestionWithAnimation() //次の問題に進む 64 } 65 66 //選択肢3をタップ 67 @IBAction func tapAnswerJ3Button(_ sender: Any) { 68 questionJData.userChoiceAnswerNumber = 3 //選択した答えの番号を保存する 69 goNextQuestionWithAnimation() //次の問題に進む 70 } 71 72 //選択肢4をタップ 73 @IBAction func tapAnswerJ4Button(_ sender: Any) { 74 questionJData.userChoiceAnswerNumber = 4 //選択した答えの番号を保存する 75 goNextQuestionWithAnimation() //次の問題に進む 76 } 77 78 //選択肢5をタップ 79 @IBAction func tapAnswerJ5Button(_ sender: Any) { 80 questionJData.userChoiceAnswerNumber = 5 //選択した答えの番号を保存する 81 goNextQuestionWithAnimation() //次の問題に進む 82 } 83 84 85 //次の問題にアニメーション付きで進む 86 func goNextQuestionWithAnimation() { 87 //正解しているか判定する 88 if questionJData.isCorrect() { 89 //正解のアニメーションを再生しながら次の問題に進む 90 goNextQuestionWithCorrectAnimation() 91 } else { 92 //不正解のアニメーションを再生しながら次の問題へ進む 93 goNextQuestionWithIncorrectAnimation() 94 } 95 } 96 97 //次の問題に正解のアニメーション付きで遷移する 98 func goNextQuestionWithCorrectAnimation() { 99 100 //アニメーション 101 UIView.animate(withDuration: 1.0, animations: { 102 //アルファ値を1.0に変化させる(初期値はstoryboardで0.0に設定済み) 103 self.correctImageView.alpha = 1.0 104 }) { 105 (Bool) in self.goNextQuestion() //アニメーション完了後に次の問題に進む 106 } 107 108 } 109 110 //次の問題に不正解のアニメーション付きで遷移する 111 func goNextQuestionWithIncorrectAnimation() { 112 113 //アニメーション 114 UIView.animate(withDuration: 1.0, animations: { 115 //アルファ値を1.0に変化させる 116 self.incorrectImageView.alpha = 1.0 117 }) { 118 (Bool) in self.goNextQuestion() //アニメーション完了後に次に問題に進む 119 } 120 } 121 //次の問題へ遷移する 122 func goNextQuestion() { 123 //問題文の取り出し 124 guard let nextQuestionJ = QuestionJDataManager.sharedInstance.nextQuestion() else { 125 //問題文がなければ次に画面に遷移する 126 //storyboardのIdentifierに設定した値(リザルト)を指定して 127 //ViewControllerを生成する 128 if let result2ViewController = storyboard?.instantiateViewController(withIdentifier: "result2") as? Result2ViewController { 129 //storyboardのsegueを利用しない明示的な画面遷移処理 130 present(result2ViewController, animated: true, completion: nil) 131 } 132 return 133 } 134 135 //問題文がある場合は次に問題に遷移する 136 //storyboardのidentifierに設定した値(question)を設定して 137 //ViewControllerを生成する 138 if let nextQuestionJViewController = storyboard?.instantiateViewController(withIdentifier: "questionJ") as? QuestionJViewController { 139 nextQuestionJViewController.questionJData = nextQuestionJ 140 //storydoardのsegueを利用しない明示的な画面遷移処理 141 present(nextQuestionJViewController, animated: true, completion: nil) 142 } 143 144 } 145 146}

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

Swift3

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

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

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

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

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

guest

回答1

0

問題数をカウントしていって50以上になったら遷移っていうのではだめなのですか?

投稿2018/02/05 03:00

iwanote

総合スコア295

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問