2016-07-15 49 views
0

我在Swift中構建遊戲來評估選擇A與選擇B相比是否爲真。遊戲對於字符串正常工作,但我想顯示圖像而不是字符串(例如對於firstQuestion.choiceA = "A",不顯示「A」而是顯示與A相關的圖像 - 對於B是相同的)。我已經看到字典的解決方案將字符串映射到圖像或提取和比較圖像名稱,但不知道如何實現在我的情況。代碼的主要部分是封閉的。有人有這樣的例子在Swift中如何做到這一點?真假遊戲中的顯示圖像選擇

import UIKit 

class ViewController: UIViewController { 

    class Questions { 
     var question : String 
     var answer : String 
     var choiceA : String 
     var choiceB : String 

     init() { 
      question = "" 
      answer = "" 
      choiceA = "" 
      choiceB = "" 
     } 

     func isCorrect(input : String) -> Bool { 
      return input == answer 
     } } 

    class QuestionGenerator { 
     var questionList : [Questions] 
     var counter : Int   
     var currentQuestion : Questions/ 

     init() { 
      questionList = [Questions]() 
      counter = 0     
      currentQuestion = Questions() 
     } 

     func addQuestion(question : Questions) { 
      questionList.append(question) 
     } 

     func getNextQuestion() -> Questions { 

      if (counter < questionList.count) { 
       currentQuestion = questionList[counter] 
       counter = counter + 1 
       return currentQuestion 
      } 

      else 
      { 
       counter = 0 
       currentQuestion = questionList[counter] 
       return currentQuestion 
      } } } 

     class Model 
    { 

     var currentQuestion : Questions 
     var questionGen : QuestionGenerator 

     init() 

     { 
      let firstQuestion = Questions() 
      firstQuestion.question = "Which one took longer?" 
      firstQuestion.choiceA = "A" 
      firstQuestion.choiceB = "B" 
      firstQuestion.answer = firstQuestion.choiceA 

      questionGen = QuestionGenerator() 
      questionGen.addQuestion(firstQuestion) 
     } 

     func isCorrect(userInput : String) -> Bool 
     { 
      return userInput == currentQuestion.answer 
     } } 

回答

0

您可以有兩個圖像視圖來顯示與故事板中的答案相關的兩個圖像。您可以通過使用asstes或url的名稱加載此圖像視圖。如果您決定使用資產的名稱,則可以使用與答案相同的名稱,也可以使用imageA和imageB將兩個字符串添加到模型中。

然後你只需加載圖像是這樣的:

imageViewA.image =的UIImage(命名爲:currentQuestion.choiceA)

0

我寫的如何實現這在操場的例子。

在finder中打開操場文件並創建一個名爲「Resources」的文件夾,在那裏放置兩個圖像(稱爲「image」和「image2」),然後將此代碼複製到操場中。

import UIKit 

enum AnswerOption { 
    case Text(String) 
    case Image(UIImage) 
} 

extension AnswerOption: Equatable { } 
func ==(lhs: AnswerOption, rhs: AnswerOption) -> Bool { 
    switch (lhs, rhs) { 
    case (let .Text(str1), let .Text(str2)): 
     return str1 == str2 
    case (let .Image(img1), let .Image(img2)): 
     return img1 == img2 
    default: 
     return false 
    } 
} 

struct Question { 
    let text: String 
    let answer: AnswerOption 
    let choiceA: AnswerOption 
    let choiceB: AnswerOption 

    init(text: String, choiceA: AnswerOption, choiceB: AnswerOption, answer: AnswerOption) { 
     self.text = text 
     self.answer = answer 
     self.choiceA = choiceA 
     self.choiceB = choiceB 
    } 

    func isCorrect(input: AnswerOption) -> Bool { 
     return input == answer 
    } 
} 

extension Question { 
    func isCorrect(input: UIImage) -> Bool { return isCorrect(.Image(input)) } 
    func isCorrect(input: String) -> Bool { return isCorrect(.Text(input)) } 
} 

extension Question { 
    init(text: String, choiceA: String, choiceB: String, answer: String) { 
     self.init(text: text, choiceA: .Text(choiceA), choiceB: .Text(choiceB), answer: .Text(answer)) 
    } 

    init(text: String, choiceA: UIImage, choiceB: UIImage, answer: UIImage) { 
     self.init(text: text, choiceA: .Image(choiceA), choiceB: .Image(choiceB), answer: .Image(answer)) 
    } 
} 

class QuestionGenerator { 
    private var questions = [Question]() 
    private var counter = 0 
    private var currentQuestion: Question? { 
     return counter < questions.count ? questions[counter] : nil 
    } 

    func addQuestion(question : Question) { 
     questions.append(question) 
    } 

    func getNextQuestion() -> Question { 
     counter += 1 
     if counter >= questions.count { 
      counter = 0 
     } 
     return currentQuestion! 
    } 
} 

// ====== Example usage ====== 

let questionGenerator = QuestionGenerator() 
questionGenerator.addQuestion(Question(text: "Which one took longer?", choiceA: "A", choiceB: "B", answer: "A")) 
questionGenerator.addQuestion(Question(text: "Which didn't take long at all?", choiceA: "A", choiceB: "B", answer: "B")) 
let image = UIImage(named: "image")! 
let image2 = UIImage(named: "image2")! 
questionGenerator.addQuestion(Question(text: "Which one is the StackOverflow logo?", choiceA: image, choiceB: image2, answer: image)) 

var question = questionGenerator.currentQuestion! 

print(question.isCorrect(question.choiceA) ? "Correct" : "Incorrect") 
question = questionGenerator.getNextQuestion() 
print(question.isCorrect("A") ? "Correct" : "Incorrect") 
question = questionGenerator.getNextQuestion() 
print(question.isCorrect(image) ? "Correct" : "Incorrect") 

枚舉AnswerOption可以是字符串或圖像,並且枚舉符合Equatable。有了這個解決方案,你可以在未來有圖像,字符串或其他任何東西。

我的例子可能有點矯枉過正,但在操場上檢查一下,也許可以從中得到一些啓發。

0
Outlet var QuestionLabel: UILabel! 

Outlet var ButtonOne: UIButton! 
Outlet var ButtonTwo: UIButton! 
Outlet var OutputLabel: UILabel! 
Action func checkAnswer(sender : UIButton) 

let userInput = sender.titleLabel!.text 

if(model.isCorrect(userInput!) == true) 

{ 

    OutputLabel.text = "Good Job" 
    let currentQuestion = model.getNextQuestion() 
    QuestionLabel.text = currentQuestion.question 
    ButtonOne.seTitle(currentQuestion.choiceA, forState:  UIControlState.Normal) 
    ButtonTwo.setTitle(currentQuestion.choiceB, forState: UIControlState.Normal) 

if (counter == 0) 
{ 
    ButtonOne.setBackgroundImage(UIImage(named:"A1"), for State: .Normal) 
    ButtonTwo.setBackgroundImage(UIImage(named:"B1"), for State: .Normal) 
} 
if (counter == 1) 
{ 
    ButtonOne.setBackgroundImage(UIImage(named:"A2"), for State: .Normal) 
    ButtonTwo.setBackgroundImage(UIImage(named:"B2"), for State: .Normal) 
} 

else 
{ 
    OutputLabel.text = "Sorry" 
}