ios - 尝试实现从 MVC 的模型部分到 View Controller 的方法

标签 ios xcode swift model-view-controller

它似乎没有按预期工作。我在 iOS 上创建了一个小测验,在这个测验中,我尝试实现一种方法,以数组的形式将不正确(错过的)问题从我的模型返回到我的 View Controller 。

这是模型中的方法:

func incorrectItems() -> [String] {
        var theAnswer:String
        var theQuestion:String
        var inCorrectAnswers = [String]()

        for (var j = 0; j < myAnswers.count; j++) {
            theQuestion = myQuestions[j]
            theAnswer = myAnswers[j]
            if (theAnswer != myAnswers[j]) {
                inCorrectAnswers.append(myQuestions[j])
            }
        }
        return inCorrectAnswers
    }

在我的 View Controller 中,我尝试实现此代码以显示在 TextView 中:

func textFieldShouldReturn(txtAnswerField: UITextField) -> Bool {
        model.recordAnswer(txtAnswerField.text!) //Have instance of model record answer from text field
        numCorrect = model.numOfCorrectAnswers //number of correct answers
        numWrong = model.numOfIncorrectAnswers //number of incorrect answers
        txtViewQuestionStatus.text = "\(model.incorrectItems())\n" //Have text view fill up with incorrect answers
        model.theQuestion++
        if model.hasMoreQuestions == true {
            model.theQuestion++
            updateQuestionAndAnswer()
        }
        txtAnswerField.text = ""
        txtAnswerField.resignFirstResponder() //hide keyboard
        return true
    }

提前谢谢您。

最佳答案

我想通了,我能够计算出我之前实现的另一种方法所需的正确代码。该方法涉及我留在另一个方法 recordAnswer(String) 中的错误代码。我继续执行条件语句,并让 inCorrectItems 方法返回所有未正确回答的答案。

谢谢大家。

关于ios - 尝试实现从 MVC 的模型部分到 View Controller 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961904/

相关文章:

php - NSMutableURLRequest : setHTTPMethod 的解释

ios - 在 Swift 中将字符串转换为数组 : Time Complexity

ios - 如何从单元测试更改应用程序的 NSProcessInfo 环境字典?

iOS - 代表不工作

ios - 如何知道 HTTPRequest 之后所有任务何时完成(Swift)

ios - 如何关闭 UIScrollView 的轻拂滚动功能

ios - PDFKit/PDFView 禁用与表单注释的交互

iOS代码覆盖率: @implementation Not Hit?

iphone - 如何为 iOS 私有(private) api (MobileWifi.framework) 添加授权

ios - 触摸 searchBar 时未调用 updateSearchResults 函数