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

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

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

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

Swift

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

Q&A

解決済

2回答

2452閲覧

少しでもいいのでアドバイスください

pokase

総合スコア16

Xcode

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

Swift

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

0グッド

1クリップ

投稿2015/12/21 13:16

編集2015/12/21 13:41

クイズアプリを作っているんですが問題がエンドレスに出てしまい、どうやったら全問といて終わるのかわかりません……

また、同じ問題が何回か出てきたりしますその直し方も教えていただきたいです。

Swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 6 @IBOutlet var QuestionLabel: UILabel! 7 8 @IBOutlet var Button1: UIButton! 9 @IBOutlet var Button2: UIButton! 10 @IBOutlet var Button3: UIButton! 11 @IBOutlet var Button4: UIButton! 12 13 @IBOutlet var LabelEnd: UILabel! 14 15 @IBOutlet var Next: UIButton! 16 var CorrectAnswer = String() 17 18 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 // Do any additional setup after loading the view, typically from a nib. 23 24 Hide() 25 26 RandomQuestions() 27 } 28 29 override func didReceiveMemoryWarning() { 30 super.didReceiveMemoryWarning() 31 // Dispose of any resources that can be recreated. 32 } 33 34 35 func RandomQuestions(){ 36 37 var RandomNumber = arc4random() % 10 38 RandomNumber += 1 39 40 switch(RandomNumber){ 41 42 43 44 case 1: 45 46 QuestionLabel.text = "" 47 Button1.setTitle("", forState: UIControlState.Normal) 48 Button2.setTitle("", forState: UIControlState.Normal) 49 Button3.setTitle("", forState: UIControlState.Normal) 50 Button4.setTitle("", forState: UIControlState.Normal) 51 CorrectAnswer = "" 52 53 54 break 55 case 2: 56 QuestionLabel.text = "" 57 Button1.setTitle("", forState: UIControlState.Normal) 58 Button2.setTitle("", forState: UIControlState.Normal) 59 Button3.setTitle("", forState: UIControlState.Normal) 60 Button4.setTitle("", forState: UIControlState.Normal) 61 CorrectAnswer = "" 62 63 64 break 65 case 3: 66 QuestionLabel.text = "" 67 Button1.setTitle("", forState: UIControlState.Normal) 68 Button2.setTitle("", forState: UIControlState.Normal) 69 Button3.setTitle("", forState: UIControlState.Normal) 70 Button4.setTitle("", forState: UIControlState.Normal) 71 CorrectAnswer = "" 72 73 74 break 75 case 4: 76 QuestionLabel.text = "" 77 Button1.setTitle("", forState: UIControlState.Normal) 78 Button2.setTitle("", forState: UIControlState.Normal) 79 Button3.setTitle("", forState: UIControlState.Normal) 80 Button4.setTitle("", forState: UIControlState.Normal) 81 CorrectAnswer = "" 82 83 84 break 85 case 5: 86 QuestionLabel.text = "" 87 Button1.setTitle("", forState: UIControlState.Normal) 88 Button2.setTitle("", forState: UIControlState.Normal) 89 Button3.setTitle("", forState: UIControlState.Normal) 90 Button4.setTitle("", forState: UIControlState.Normal) 91 CorrectAnswer = "" 92 93 94 break 95 case 6: 96 QuestionLabel.text = "" 97 Button1.setTitle("", forState: UIControlState.Normal) 98 Button2.setTitle("", forState: UIControlState.Normal) 99 Button3.setTitle("", forState: UIControlState.Normal) 100 Button4.setTitle("", forState: UIControlState.Normal) 101 CorrectAnswer = "" 102 break 103 104 105 case 7: 106 QuestionLabel.text = "" 107 Button1.setTitle("", forState: UIControlState.Normal) 108 Button2.setTitle("", forState: UIControlState.Normal) 109 Button3.setTitle("", forState: UIControlState.Normal) 110 Button4.setTitle("", forState: UIControlState.Normal) 111 CorrectAnswer = "" 112 break 113 114 115 default: 116 117 break 118 119 } 120 } 121 122 func Hide(){ 123 LabelEnd.hidden = true 124 Next.hidden = true 125 } 126 127 func UnHide(){ 128 LabelEnd.hidden = false 129 Next.hidden = false 130 } 131 132 133 134 135 136 @IBAction func Button1Action(sender: AnyObject) { 137 138 UnHide() 139 140 if (CorrectAnswer == "1"){ 141 142 LabelEnd.text = "正解だよ!" 143 } 144 else{ 145 LabelEnd.text = "ザンネーン!!" 146 } 147 148 149 } 150 151 @IBAction func Button2Action(sender: AnyObject) { 152 153 UnHide() 154 155 if (CorrectAnswer == "2"){ 156 157 LabelEnd.text = "正解だよ!" 158 } 159 else{ 160 LabelEnd.text = "ザンネーン!!" 161 } 162 } 163 164 165 @IBAction func Button3Action(sender: AnyObject) { 166 167 UnHide() 168 169 if (CorrectAnswer == "3"){ 170 171 LabelEnd.text = "正解だよ!" 172 } 173 else{ 174 LabelEnd.text = "ザンネーン!!" 175 } 176 177 } 178 179 @IBAction func Button4Action(sender: AnyObject) { 180 181 UnHide() 182 183 if (CorrectAnswer == "4"){ 184 185 LabelEnd.text = "正解だよ!" 186 } 187 else{ 188 LabelEnd.text = "ザンネーン!!" 189 } 190 } 191 192 193 194 @IBAction func Next(sender: AnyObject) { 195 196 RandomQuestions() 197 Hide() 198 199 200 } 201 202 }

長文失礼します

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

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

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

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

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

ikuwow

2015/12/22 00:25

こちらの質問が他のユーザから「質問の範囲が広すぎる」という評価を受けています わからない点を明確にし、調査したこと・試したことと共に記入していただくと、回答が得られやすくなります。
mazinai_tarazu

2015/12/22 08:53

こちらの質問が他のユーザから「質問の範囲が広すぎる」という評価を受けています わからない点を明確にし、調査したこと・試したことと共に記入していただくと、回答が得られやすくなります。
guest

回答2

0

ベストアンサー

載せているコードを参考にして、簡単なサンプルを作ってみたので試してみてください。
実装的にはStripeさんのアドバイスの通りになっています。
分からないところがあったら質問してください。

Quiz Github

投稿2015/12/21 15:20

編集2015/12/21 15:23
_Kentarou

総合スコア8490

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

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

pokase

2015/12/21 15:26

ごめんなさい、どしたらXcodeで見れますか?
pokase

2015/12/21 15:27

すいませんでした。 見れました本当にありがとうございました
pokase

2015/12/21 15:33

シミュレーションで試したいのですがどうすれば動きますか?
_Kentarou

2015/12/21 15:37 編集

シュミレーターでそのまま実行できるとおもいますが、エラーにでもなってますか? ちなみにこちらの環境はXcode7.2です。
pokase

2015/12/21 15:39

A build only device cannot be used to run this target. こんなのがでます…
pokase

2015/12/21 16:44

すいません アップデートしたらうまくいきました。 あと、解き終わったらホームに行くようにしてみたいんですけど import UIKit class CollectionViewController: UICollectionViewController { var Array = [String]() var ButtonArray = [String]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. Array = ["平成27年","平成27年","平成26年","平成26年","平成25年","平成25年","平成24年","平成24年","平成23年","平成23年","平成22年","平成22年"] ButtonArray = ["上期","下期","上期","下期","上期","下期","上期","下期","上期","下期","上期","下期"] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return Array.count } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell let label = cell.viewWithTag(1) as! UILabel label.text = Array[indexPath.row] let Button = cell.viewWithTag(2) as! UIButton Button.setTitle(ButtonArray[indexPath.row], forState: UIControlState.Normal) return cell } } ホーム画面です
_Kentarou

2015/12/21 17:04

画面遷移のイメージが付かないのですが、戻るということならば以下のコードで戻ればよいと思います。 // Navigation遷移の場合 self.navigationController?.popViewControllerAnimated(true) // Modal遷移の場合 dismissViewControllerAnimated(true, completion: nil) 動きとしては、全てのクイズが終わった時にアラートなりを出してボタン押下で戻るとかがよいかと思います。
pokase

2015/12/22 11:31

問題の画面の最後にコードをかけばいいのでしょうか?
_Kentarou

2015/12/22 13:22

pokaseさんが考えて作られているアプリなので、自分が戻りたいタイミングで上記のコードを書けば良いと思います。
pokase

2015/12/22 14:13

色々試してみます… たくさんの質問に答えていただきありがとうございました。
guest

0

まず、問題番号の配列を用意します。
次に、その配列の中身をシャッフル(ランダムに並べ替え)します。
そして、配列から一つずつ問題番号を取り出して(抜き取って)、その問題を出題します。
配列が空っぽになれば、終了です。

投稿2015/12/21 14:15

Stripe

総合スコア2183

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

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

pokase

2015/12/21 14:36

サンプル的なのないですか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問