添加图像(在代码中)以便在视图控制器上显示图像

Adding an image (in code) in order show an image on the view controller

我有这个测验应用程序,用户会看到一个问题和四个选项。这些选项在 table 视图函数中,标签通过每个问题的代码更改。我想添加基于图像的问题。我的资产中有一些图片,但是当我尝试

image.UIImageView 这是一个错误。

我希望当用户调用新的问题和答案时,在图像视图中显示一个新图像。

这是代码,以及提供的视图控制器屏幕截图。


import UIKit

class imagequizpage1: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var label: UILabel!
    @IBOutlet var image: UIImageView!
    @IBOutlet var table: UITableView!
    
    var gameModels = [Question]()
    
    var currentQuestion: Question?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        table.delegate = self
        table.dataSource = self
        setupQuestions()
        configureUI(question: gameModels.first!)
        
    }
    
    private func configureUI(question: Question) {
    label.text = question.text
    currentQuestion = question
    table.reloadData()
    }
    
    private func checkAnswer(answer: Answer, question: Question) -> Bool{
        return question.answers.contains(where: { [=10=].text == answer.text }) && answer.correct
    }
    
    
    private func setupQuestions(){
    gameModels.append(Question(text: "What is this image", answers: [
        Answer(text: "A bridge", correct: true),
        Answer(text: "A waterway.", correct: false),
        Answer(text: "A skyline", correct: false),
        Answer(text: "A park", correct: false)
    ]))
        
        
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentQuestion?.answers.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = currentQuestion?.answers[indexPath.row].text
            cell.textLabel?.numberOfLines = 0
            return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    
    guard let question = currentQuestion else {
        return
    }
    
    let answer = question.answers[indexPath.row]
    
    if checkAnswer(answer: answer, question: question) {
        // correct
        if let index = gameModels.firstIndex(where: { [=10=].text == question.text }) {
            if index < (gameModels.count - 1){
                // next question
                let nextQuestion = gameModels[index + 1]
                currentQuestion = nil
                configureUI(question: nextQuestion)
            
                }
                else{
                    let alert = UIAlertController(title: "Done", message: "You beat the game" , preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
                    present(alert, animated: true)
                }
           }
        }
        else{
            let alert = UIAlertController(title: "Wrong", message: "That's wrong", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
            present(alert, animated: true)
        }
        
}

    struct Question {
        let text: String
        let answers: [Answer]
}

    struct Answer {
        let text: String
        let correct: Bool
}
}

[

非常感谢。

您需要将图片设置为imageView

imageView.image = UIImage(named: "Asset Name")

注意:更改您的 UIImageView 名称 ... 而不是 image 使用 imageView