質問編集履歴
1
やり方を変えてみました。csvでなく、if文を使ってしました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
swift
|
1
|
+
swift クイズアプリでボタンを押したら画像の切り替える方法
|
test
CHANGED
@@ -12,17 +12,19 @@
|
|
12
12
|
|
13
13
|
### 発生している問題・エラーメッセージ画像の追加まではできましたが、画像の追加のコードを入力してからエラーがでました。
|
14
14
|
|
15
|
+
前回の投稿から違うやり方でしてみました。
|
16
|
+
|
15
|
-
|
17
|
+
UIImageViewを接続し、if文を使って画像の切り替えをしてるのがあったのでやってみました。
|
18
|
+
|
19
|
+
画像のコードを入力しましたが、エラーの意味もわからず何が間違っているのか分かりません。
|
20
|
+
|
21
|
+
|
16
22
|
|
17
23
|
|
18
24
|
|
19
25
|
エラーメッセージ
|
20
26
|
|
21
|
-
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
QuizViewControllerの値にはメンバーがありません。
|
26
28
|
|
27
29
|
|
28
30
|
|
@@ -58,9 +60,51 @@
|
|
58
60
|
|
59
61
|
@IBOutlet weak var judgeImageView: UIImageView!
|
60
62
|
|
63
|
+
|
64
|
+
|
65
|
+
**〜〜〜変更部分〜〜〜**
|
66
|
+
|
67
|
+
|
68
|
+
|
61
|
-
@IBOutlet weak var appleImage: UIImageView!
|
69
|
+
** @IBOutlet weak var appleImageView: UIImageView!
|
70
|
+
|
71
|
+
|
72
|
+
|
62
|
-
|
73
|
+
func changeImage(someValue: Int) {
|
74
|
+
|
63
|
-
|
75
|
+
if someValue == 0 {
|
76
|
+
|
77
|
+
self.imageView.image = UIImage(named:"first_image")
|
78
|
+
|
79
|
+
} else if someValue == 1 {
|
80
|
+
|
81
|
+
self.imageView.image = UIImage(named:"second_image")
|
82
|
+
|
83
|
+
} else if someValue == 2 {
|
84
|
+
|
85
|
+
self.imageView.image = UIImage(named:"third_image")
|
86
|
+
|
87
|
+
} else if someValue == 3 {
|
88
|
+
|
89
|
+
self.imageView.image = UIImage(named:"fourth_image")
|
90
|
+
|
91
|
+
} else if someValue == 4 {
|
92
|
+
|
93
|
+
self.imageView.image = UIImage(named:"some_image")
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
**
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
**〜〜〜ここまで〜〜〜**
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
64
108
|
|
65
109
|
//csvファイルを読み込む・問題文と選択肢を表示する
|
66
110
|
|
@@ -164,212 +208,188 @@
|
|
164
208
|
|
165
209
|
|
166
210
|
|
167
|
-
|
168
|
-
|
211
|
+
|
212
|
+
|
169
|
-
|
213
|
+
//選択肢を表示する
|
214
|
+
|
170
|
-
|
215
|
+
answerBtn1.setTitle(quizArray[2], for: .normal)
|
216
|
+
|
171
|
-
|
217
|
+
answerBtn2.setTitle(quizArray[3], for: .normal)
|
218
|
+
|
172
|
-
|
219
|
+
answerBtn3.setTitle(quizArray[4], for: .normal)
|
220
|
+
|
173
|
-
|
221
|
+
answerBtn4.setTitle(quizArray[5], for: .normal)
|
222
|
+
|
223
|
+
|
224
|
+
|
174
|
-
|
225
|
+
// Do any additional setup after loading the view.
|
226
|
+
|
175
|
-
|
227
|
+
}
|
176
|
-
|
228
|
+
|
229
|
+
|
230
|
+
|
177
|
-
|
231
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
|
232
|
+
|
178
|
-
|
233
|
+
let sVC = segue.destination as! ScoreViewController
|
234
|
+
|
235
|
+
sVC.correct = correctCount
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
//ボタンをタップした時の判定
|
242
|
+
|
243
|
+
@IBAction func btnAction(sender: UIButton) {
|
244
|
+
|
245
|
+
if sender.tag == Int(quizArray[1]) {
|
246
|
+
|
247
|
+
correctCount += 1
|
248
|
+
|
179
|
-
|
249
|
+
judgeImageView.image = UIImage(named: "correct")
|
250
|
+
|
180
|
-
|
251
|
+
} else {
|
252
|
+
|
253
|
+
judgeImageView.image = UIImage(named: "incorrect")
|
254
|
+
|
255
|
+
}
|
256
|
+
|
181
|
-
|
257
|
+
judgeImageView.isHidden = false
|
258
|
+
|
259
|
+
explanation()
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
//戻るボタン
|
266
|
+
|
267
|
+
@objc func backBtnTapped(){
|
268
|
+
|
269
|
+
let screenHeight = Double(UIScreen.main.bounds.size.height)
|
270
|
+
|
271
|
+
UIView.animate(withDuration: 0.5, animations: {() -> Void in self.explanetionBGView.frame = CGRect(x: self.explanationBGX, y: screenHeight, width: 320, height: 210)
|
272
|
+
|
273
|
+
})
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
answerBtn1.isEnabled = true
|
278
|
+
|
279
|
+
answerBtn2.isEnabled = true
|
280
|
+
|
281
|
+
answerBtn3.isEnabled = true
|
282
|
+
|
283
|
+
answerBtn4.isEnabled = true
|
284
|
+
|
285
|
+
answerBtn5.isEnabled = true
|
286
|
+
|
287
|
+
judgeImageView.isHidden = true
|
288
|
+
|
289
|
+
nextQuiz()
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
func explanation(){
|
300
|
+
|
301
|
+
let correctNumber = Int(quizArray[1])!
|
302
|
+
|
303
|
+
correctLabel.text = "答え:(quizArray[correctNumber + 2])"
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
let answerBtnY = answerBtn1.frame.origin.y
|
308
|
+
|
309
|
+
UIView.animate(withDuration: 0.5, animations: {() -> Void in self.explanetionBGView.frame = CGRect(x: self.explanationBGX, y: Double(answerBtnY), width: 320, height: 280)
|
310
|
+
|
311
|
+
})
|
312
|
+
|
313
|
+
answerBtn1.isEnabled = false
|
314
|
+
|
315
|
+
answerBtn2.isEnabled = false
|
316
|
+
|
317
|
+
answerBtn3.isEnabled = false
|
318
|
+
|
319
|
+
answerBtn4.isEnabled = false
|
320
|
+
|
321
|
+
answerBtn5.isEnabled = false
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
//次の問題を表示
|
328
|
+
|
329
|
+
func nextQuiz() {
|
330
|
+
|
331
|
+
quizCount += 1
|
182
332
|
|
183
333
|
|
184
334
|
|
185
335
|
|
186
336
|
|
187
|
-
**ーーーここまでーーー**
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
//
|
337
|
+
//画面遷移
|
338
|
+
|
194
|
-
|
339
|
+
quizArray.removeAll()
|
340
|
+
|
341
|
+
if quizCount < quizTotal {
|
342
|
+
|
343
|
+
quizArray = csvArray[quizCount].components(separatedBy: ",")
|
344
|
+
|
345
|
+
quizNumberLabel.text = "第(quizCount + 1)門"
|
346
|
+
|
347
|
+
quizTextView.text = quizArray[0]
|
348
|
+
|
195
|
-
answerBtn1.setTitle(quizArray[2], for: .normal)
|
349
|
+
answerBtn1.setTitle(quizArray[2], for: .normal)
|
196
|
-
|
350
|
+
|
197
|
-
answerBtn2.setTitle(quizArray[3], for: .normal)
|
351
|
+
answerBtn2.setTitle(quizArray[3], for: .normal)
|
198
|
-
|
352
|
+
|
199
|
-
answerBtn3.setTitle(quizArray[4], for: .normal)
|
353
|
+
answerBtn3.setTitle(quizArray[4], for: .normal)
|
200
|
-
|
354
|
+
|
201
|
-
answerBtn4.setTitle(quizArray[5], for: .normal)
|
355
|
+
answerBtn4.setTitle(quizArray[5], for: .normal)
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
356
|
+
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
|
212
|
-
|
213
|
-
let sVC = segue.destination as! ScoreViewController
|
214
|
-
|
215
|
-
sVC.correct = correctCount
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
//ボタンをタップした時の判定
|
222
|
-
|
223
|
-
@IBAction func btnAction(sender: UIButton) {
|
224
|
-
|
225
|
-
|
357
|
+
answerBtn5.setTitle(quizArray[6], for: .normal)
|
226
|
-
|
227
|
-
|
358
|
+
|
228
|
-
|
229
|
-
|
359
|
+
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
230
364
|
|
231
365
|
} else {
|
232
366
|
|
233
|
-
|
367
|
+
performSegue(withIdentifier: "scoreVC", sender: nil)
|
234
368
|
|
235
369
|
}
|
236
370
|
|
237
|
-
judgeImageView.isHidden = false
|
238
|
-
|
239
|
-
explanation()
|
240
|
-
|
241
|
-
}
|
371
|
+
}
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
372
|
+
|
246
|
-
|
373
|
+
|
374
|
+
|
247
|
-
|
375
|
+
func loadCSV(fileName: String) -> [String] {
|
376
|
+
|
248
|
-
|
377
|
+
let csvBundle = Bundle.main.path(forResource: fileName, ofType: "csv")!
|
378
|
+
|
379
|
+
do {
|
380
|
+
|
249
|
-
let s
|
381
|
+
let csvDate = try String(contentsOfFile: csvBundle,encoding: String.Encoding.utf8)
|
250
|
-
|
251
|
-
|
382
|
+
|
252
|
-
|
253
|
-
})
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
answerBtn1.isEnabled = true
|
258
|
-
|
259
|
-
answerBtn2.isEnabled = true
|
260
|
-
|
261
|
-
answerBtn3.isEnabled = true
|
262
|
-
|
263
|
-
answerBtn4.isEnabled = true
|
264
|
-
|
265
|
-
answerBtn5.isEnabled = true
|
266
|
-
|
267
|
-
judgeImageView.isHidden = true
|
268
|
-
|
269
|
-
nextQuiz()
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
func explanation(){
|
280
|
-
|
281
|
-
let correctNumber = Int(quizArray[1])!
|
282
|
-
|
283
|
-
|
383
|
+
let lineChange = csvDate.replacingOccurrences(of: "\r", with: "\n")
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
384
|
+
|
288
|
-
|
289
|
-
UIView.animate(withDuration: 0.5, animations: {() -> Void in self.explanetionBGView.frame = CGRect(x: self.explanationBGX, y: Double(answerBtnY), width: 320, height: 280)
|
290
|
-
|
291
|
-
})
|
292
|
-
|
293
|
-
answerBtn1.isEnabled = false
|
294
|
-
|
295
|
-
answerBtn2.isEnabled = false
|
296
|
-
|
297
|
-
answerBtn3.isEnabled = false
|
298
|
-
|
299
|
-
answerBtn4.isEnabled = false
|
300
|
-
|
301
|
-
answerBtn5.isEnabled = false
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
//次の問題を表示
|
308
|
-
|
309
|
-
func nextQuiz() {
|
310
|
-
|
311
|
-
quizCount += 1
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
//画面遷移
|
318
|
-
|
319
|
-
quizArray.removeAll()
|
320
|
-
|
321
|
-
if quizCount < quizTotal {
|
322
|
-
|
323
|
-
|
385
|
+
csvArray = lineChange.components(separatedBy: "\n")
|
324
|
-
|
325
|
-
|
386
|
+
|
326
|
-
|
327
|
-
quizTextView.text = quizArray[0]
|
328
|
-
|
329
|
-
answerBtn1.setTitle(quizArray[2], for: .normal)
|
330
|
-
|
331
|
-
answerBtn2.setTitle(quizArray[3], for: .normal)
|
332
|
-
|
333
|
-
answerBtn3.setTitle(quizArray[4], for: .normal)
|
334
|
-
|
335
|
-
answerBtn4.setTitle(quizArray[5], for: .normal)
|
336
|
-
|
337
|
-
answerBtn5.setTitle(quizArray[6], for: .normal)
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
}
|
387
|
+
} catch {
|
346
|
-
|
388
|
+
|
347
|
-
p
|
389
|
+
print("エラー")
|
348
390
|
|
349
391
|
}
|
350
392
|
|
351
|
-
}
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
func loadCSV(fileName: String) -> [String] {
|
356
|
-
|
357
|
-
let csvBundle = Bundle.main.path(forResource: fileName, ofType: "csv")!
|
358
|
-
|
359
|
-
do {
|
360
|
-
|
361
|
-
let csvDate = try String(contentsOfFile: csvBundle,encoding: String.Encoding.utf8)
|
362
|
-
|
363
|
-
let lineChange = csvDate.replacingOccurrences(of: "\r", with: "\n")
|
364
|
-
|
365
|
-
csvArray = lineChange.components(separatedBy: "\n")
|
366
|
-
|
367
|
-
} catch {
|
368
|
-
|
369
|
-
print("エラー")
|
370
|
-
|
371
|
-
}
|
372
|
-
|
373
393
|
return csvArray
|
374
394
|
|
375
395
|
}
|
@@ -408,7 +428,7 @@
|
|
408
428
|
|
409
429
|
|
410
430
|
|
411
|
-
|
431
|
+
ご教授頂いたことをしているのか、わかりませんがネットで似ているのを探していれこみました。すみませんが、教えて下さい。お願いします。
|
412
432
|
|
413
433
|
|
414
434
|
|
@@ -416,13 +436,7 @@
|
|
416
436
|
|
417
437
|
### 補足情報(FW/ツールのバージョンなど)
|
418
438
|
|
419
|
-
|
420
|
-
|
421
|
-
csvの中はこれです。
|
422
|
-
|
423
|
-
画像にはりんごの写真が入ってます。
|
424
|
-
|
425
|
-
![イメージ説明](
|
439
|
+
![イメージ説明](5bebae0a90e3cdee5ee0b9baa6ae6b59.png)
|
426
440
|
|
427
441
|
|
428
442
|
|